Skip to content

Instantly share code, notes, and snippets.

@thinkerbot
Created May 18, 2010 20:18
Show Gist options
  • Select an option

  • Save thinkerbot/405484 to your computer and use it in GitHub Desktop.

Select an option

Save thinkerbot/405484 to your computer and use it in GitHub Desktop.
websocket example (www.igvita.com)
<html>
<head>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'></script>
<script>
var Tap = {};
$(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("close"); };
ws.onopen = function() { debug("open"); };
Tap.socket = ws;
});
</script>
</head>
<body>
<h1>Debug</h1>
<div id="debug"></div>
<h1>Console</h1>
<div id="msg"></div>
<form onsubmit="Tap.socket.send($('input').attr('value'));return false;">
<input id='input' type="text"></input>
<input type="submit" />
</form>
</body>
</html>
require 'em-websocket'
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
ws.onopen { puts "WebSocket open"; ws.send "Open"}
ws.onmessage do |msg|
if msg == "close"
ws.send "Close"
ws.close_connection(true)
else
ws.send "Got: #{msg}"
end
end
ws.onclose { puts "WebSocket closed" }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment