Class: GitGameShow::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/git_game_show/network/server.rb

Overview

WebSocket server management

Instance Attribute Summary collapse

Instance Method Summary collapse

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, message_handler)
  @port = port
  @message_handler = message_handler
end

Instance Attribute Details

#portObject (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 broadcast_message(message, exclude: nil)
  return if message.nil?

  begin
    # Convert message to JSON safely
    json_message = nil
    begin
      json_message = message.to_json
    rescue => e
      # Try to simplify the message to make it JSON-compatible
      simplified_message = {
        type: message[:type] || "unknown",
        message: "Error processing full message"
      }
      json_message = simplified_message.to_json
    end

    return unless json_message

    @message_handler.broadcast(json_message, exclude)
  rescue => e
    # Silently fail for now
  end
end

#startObject



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.onmessage do |msg|
      @message_handler.handle_message(ws, msg)
    end

    ws.onclose do
      @message_handler.handle_player_disconnect(ws)
    end
  end
end