Skip to content

Instantly share code, notes, and snippets.

@jasonmadigan
Created August 17, 2010 18:43
Show Gist options
  • Save jasonmadigan/531388 to your computer and use it in GitHub Desktop.
Save jasonmadigan/531388 to your computer and use it in GitHub Desktop.
WebSocket Test
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title>
Websocket Test
</title>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js' type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
function debug(str){ $("#debug").append("<p>" + str + "<\/p>"); };
ws = new WebSocket("ws://localhost:8080/websocket");
ws.onmessage = function(evt) { $("#msg").append("<p>" + evt.data + "<\/p>"); };
ws.onclose = function() { debug("socket closed"); };
ws.onopen = function() {
debug("connected...");
ws.send("hello server");
};
});
</script>
</head>
<body>
<div id="debug"></div>
<div id="msg"></div>
</body>
</html>
#!/usr/bin/env ruby
require 'rubygems'
require 'em-websocket'
EventMachine.run {
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
ws.onopen {
puts "WebSocket connection open"
# Push to client
ws.send "Ping!"
}
ws.onclose { puts "WebSocket closed" }
ws.onmessage { |msg|
puts "Recieved: #{msg}"
ws.send "Pong: #{msg}"
}
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment