1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#!/usr/bin/env ruby
require 'rubygems' # or use Bundler.setup
require 'eventmachine'
class SimpleChatServer < EM::Connection
@@connected_clients = Array.new
#
# EventMachine handlers
#
def post_init
@@connected_clients.push(self)
puts "A client has connected..."
end
def unbind
@@connected_clients.delete(self)
puts "A client has left..."
end
#
# Helpers
#
def other_peers
@@connected_clients.reject { |c| self == c }
end # other_peers
end
EventMachine.run do
# hit Control + C to stop
Signal.trap("INT") { EventMachine.stop }
Signal.trap("TERM") { EventMachine.stop }
EventMachine.start_server("0.0.0.0", 10000, SimpleChatServer)
end
|