Add event machine websocket server
This commit is contained in:
parent
9f8fce38ce
commit
bfe77a7c2e
@ -10,6 +10,7 @@ require_relative "server/message_parser"
|
||||
require_relative "server/matching_engine"
|
||||
require_relative "server/message_broker"
|
||||
require_relative "server/message_processor"
|
||||
require_relative "server/websocket_server"
|
||||
module Rex
|
||||
module Server
|
||||
VERSION = "0.1.0"
|
||||
|
33
lib/rex/server/websocket_server.rb
Normal file
33
lib/rex/server/websocket_server.rb
Normal file
@ -0,0 +1,33 @@
|
||||
require "em-websocket"
|
||||
module Rex
|
||||
module Server
|
||||
class WebsocketServer
|
||||
def initialize(message_broker, outbox, host: "0.0.0.0", port: 8080)
|
||||
@message_broker = message_broker
|
||||
@outbox = outbox
|
||||
@host = host
|
||||
@port = port
|
||||
end
|
||||
|
||||
def start
|
||||
EM::WebSocket.run(host: @host, port: @port) do |connection|
|
||||
setup_connection_handling(connection)
|
||||
end
|
||||
end
|
||||
|
||||
def setup_connection_handling(connection)
|
||||
connection.onopen do
|
||||
connection_id = @message_broker.register(connection)
|
||||
|
||||
connection.onmessage do |message|
|
||||
@outbox.push([connection_id, message])
|
||||
end
|
||||
|
||||
connection.onclose do
|
||||
@message_broker.unregister(connection_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user