Skip to content

Instantly share code, notes, and snippets.

@deedubs
Created July 28, 2010 19:58
Show Gist options
  • Save deedubs/496041 to your computer and use it in GitHub Desktop.
Save deedubs/496041 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'em-websocket'
class ClientConnection
attr_accessor :socket, :name
def initialize(args)
self.socket = args[:socket]
self.name = args[:name]
end
def send(message)
self.socket.send(message)
end
end
clients = []
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
ws.onopen {
puts 'Connected'
new_client = ClientConnection.new({:socket => ws, :name => 'JohnDoe'})
clients << new_client
clients.each{|c| c.send "{\"message\": \"Someone Joined\", \"user\":\"#{new_client.name}\"}"}
}
ws.onmessage { |msg| clients.each{|c| c.send "{\"message\": \"#{msg}\"}" } }
ws.onclose { puts 'Disconnected' }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment