Created
September 2, 2011 00:13
-
-
Save dmateos/1187643 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 '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