Skip to content

Instantly share code, notes, and snippets.

@dmateos
Created September 2, 2011 00:13
Show Gist options
  • Save dmateos/1187643 to your computer and use it in GitHub Desktop.
Save dmateos/1187643 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'em-websocket'
clients = {}
EventMachine::WebSocket.start(:host => "localhost", :port =>8080) do |socket|
puts "new connection"
socket.onopen do
socket.send "hello client"
clients[socket.object_id] = socket
clients.each do |key, c|
c.send "#{socket.object_id} has connected"
end
end
socket.onmessage do |msg|
clients.each do |key, c|
c.send "#{socket.object_id}: #{msg}"
end
end
socket.onclose do
puts "closed connection"
clients.delete(socket.object_id)
clients.each do |key, c|
c.send "#{socket.object_id} has disconnected"
end
end
socket.onerror do
puts "error"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment