38 lines
751 B
Ruby
38 lines
751 B
Ruby
require 'em-websocket'
|
|
|
|
module JamWebsockets
|
|
|
|
class Server
|
|
|
|
def initialize(options={})
|
|
@log = Logging.logger[self]
|
|
@count=0
|
|
@router = Router.new :transport => self
|
|
end
|
|
|
|
def run(options={})
|
|
|
|
host = "0.0.0.0"
|
|
port = options[:port]
|
|
|
|
@log.info "starting server #{host}:#{port}"
|
|
|
|
@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.run {
|
|
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
|