Add event machine websocket server

This commit is contained in:
Tim Kächele 2024-02-18 21:15:55 +01:00
parent 9f8fce38ce
commit bfe77a7c2e
2 changed files with 34 additions and 0 deletions

View File

@ -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"

View 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