Class: GitGameShow::Server
- Inherits:
-
Object
- Object
- GitGameShow::Server
- Defined in:
- lib/git_game_show/network/server.rb
Overview
WebSocket server management
Instance Attribute Summary collapse
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #broadcast_message(message, exclude: nil) ⇒ Object
-
#initialize(port, message_handler) ⇒ Server
constructor
A new instance of Server.
- #start ⇒ Object
Constructor Details
#initialize(port, message_handler) ⇒ Server
Returns a new instance of Server.
6 7 8 9 |
# File 'lib/git_game_show/network/server.rb', line 6 def initialize(port, ) @port = port = end |
Instance Attribute Details
#port ⇒ Object (readonly)
Returns the value of attribute port.
4 5 6 |
# File 'lib/git_game_show/network/server.rb', line 4 def port @port end |
Instance Method Details
#broadcast_message(message, exclude: nil) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/git_game_show/network/server.rb', line 27 def (, exclude: nil) return if .nil? begin # Convert message to JSON safely = nil begin = .to_json rescue => e # Try to simplify the message to make it JSON-compatible = { type: [:type] || "unknown", message: "Error processing full message" } = .to_json end return unless .broadcast(, exclude) rescue => e # Silently fail for now end end |
#start ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/git_game_show/network/server.rb', line 11 def start WebSocket::EventMachine::Server.start(host: '0.0.0.0', port: @port) do |ws| ws.onopen do # Connection is logged when a player successfully joins end ws. do |msg| .(ws, msg) end ws.onclose do .handle_player_disconnect(ws) end end end |