Skip to content

Instantly share code, notes, and snippets.

@mogigoma
Created May 5, 2014 19:48
Show Gist options
  • Save mogigoma/ab7a7fe00f653e1b1f96 to your computer and use it in GitHub Desktop.
Save mogigoma/ab7a7fe00f653e1b1f96 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="demo.js"></script>
<title>WebSockets Demo</title>
</head>
<body>
<div id="wrapper">
<h1>WebSockets Demo</h1>
<div id="container">
</div>
</div>
</body>
</html>
$(document).ready(function() {
if (!('WebSocket' in window)) {
return;
}
var log = function(msg) {
$('<p>' + msg + '</p>').appendTo('#container');
}
try {
var socket = new WebSocket('ws://localhost:4444/');
socket.onopen = function(){
log('Socket status: ' + socket.readyState + ' (open).');
socket.send("You're a garbageman!");
}
socket.onmessage = function(msg){
log('Socket received: {' + msg.data + '}.');
socket.send("You're a garbageman!");
}
socket.onclose = function(){
log('Socket status: ' + socket.readyState + ' (closed).');
}
}
catch(exception) {
log('Exception: ' + exception + '.');
}
});
require 'em-websocket'
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 4444) do |ws|
ws.onopen { ws.send "Hello Client!" }
ws.onmessage { |msg| puts msg; ws.send "I know you are but what am I?" }
ws.onclose { puts "WebSocket closed" }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment