jam-cloud/lib/jam_websockets/server.rb

38 lines
775 B
Ruby

require 'em-websocket'
module JamWebsockets
class Server
def initialize(options={})
@log = Logging.logger[self]
@count=0
@router = Router.new
end
def run(options={})
host = "0.0.0.0"
port = options[:port]
@log.info "starting server #{host}:#{port}"
EventMachine.run do
@router.start
# if you don't do this, the app won't exit unless you kill -9
at_exit do
@log.info "cleaning up server"
@router.cleanup
end
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => options[:port], :debug => options[:emwebsocket_debug]) do |ws|
@log.info "new client #{ws}"
@router.new_client(ws)
end
end
end
end
end