Created
May 5, 2014 19:48
-
-
Save mogigoma/ab7a7fe00f653e1b1f96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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 + '.'); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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