Class: Tina4::WebSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/tina4/websocket.rb

Constant Summary collapse

GUID =
"258EAFA5-E914-47DA-95CA-5AB5DC11AD37"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWebSocket

Returns a new instance of WebSocket.



13
14
15
16
17
18
19
20
21
22
# File 'lib/tina4/websocket.rb', line 13

def initialize
  @connections = {}
  @handlers = {
    open: [],
    message: [],
    close: [],
    error: []
  }
  @rooms = {}  # room_name => Set of conn_ids
end

Instance Attribute Details

#connectionsObject (readonly)

Returns the value of attribute connections.



11
12
13
# File 'lib/tina4/websocket.rb', line 11

def connections
  @connections
end

Instance Method Details

#broadcast(message, exclude: nil, path: nil) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/tina4/websocket.rb', line 81

def broadcast(message, exclude: nil, path: nil)
  @connections.each do |id, conn|
    next if exclude && id == exclude
    next if path && conn.path != path
    conn.send_text(message)
  end
end

#broadcast_to_room(room_name, message, exclude: nil) ⇒ Object



109
110
111
112
113
114
# File 'lib/tina4/websocket.rb', line 109

def broadcast_to_room(room_name, message, exclude: nil)
  (get_room_connections(room_name)).each do |conn|
    next if exclude && conn.id == exclude
    conn.send_text(message)
  end
end

#get_room_connections(room_name) ⇒ Object



104
105
106
107
# File 'lib/tina4/websocket.rb', line 104

def get_room_connections(room_name)
  ids = @rooms[room_name] || Set.new
  ids.filter_map { |id| @connections[id] }
end

#handle_upgrade(env, socket) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tina4/websocket.rb', line 33

def handle_upgrade(env, socket)
  key = env["HTTP_SEC_WEBSOCKET_KEY"]
  return unless key

  accept = Base64.strict_encode64(
    Digest::SHA1.digest("#{key}#{GUID}")
  )

  response = "HTTP/1.1 101 Switching Protocols\r\n" \
             "Upgrade: websocket\r\n" \
             "Connection: Upgrade\r\n" \
             "Sec-WebSocket-Accept: #{accept}\r\n\r\n"

  socket.write(response)

  conn_id = SecureRandom.hex(16)
  ws_path = env["REQUEST_PATH"] || env["PATH_INFO"] || "/"
  connection = WebSocketConnection.new(conn_id, socket, ws_server: self, path: ws_path)
  @connections[conn_id] = connection

  emit(:open, connection)

  Thread.new do
    begin
      loop do
        frame = connection.read_frame
        break unless frame

        case frame[:opcode]
        when 0x1 # Text
          emit(:message, connection, frame[:data])
        when 0x8 # Close
          break
        when 0x9 # Ping
          connection.send_pong(frame[:data])
        end
      end
    rescue => e
      emit(:error, connection, e)
    ensure
      @connections.delete(conn_id)
      remove_from_all_rooms(conn_id)
      emit(:close, connection)
      socket.close rescue nil
    end
  end
end

#join_room_for(conn_id, room_name) ⇒ Object

── Rooms ──────────────────────────────────────────────────



91
92
93
94
# File 'lib/tina4/websocket.rb', line 91

def join_room_for(conn_id, room_name)
  @rooms[room_name] ||= Set.new
  @rooms[room_name].add(conn_id)
end

#leave_room_for(conn_id, room_name) ⇒ Object



96
97
98
# File 'lib/tina4/websocket.rb', line 96

def leave_room_for(conn_id, room_name)
  @rooms[room_name]&.delete(conn_id)
end

#on(event, &block) ⇒ Object



24
25
26
# File 'lib/tina4/websocket.rb', line 24

def on(event, &block)
  @handlers[event.to_sym] << block if @handlers.key?(event.to_sym)
end

#room_count(room_name) ⇒ Object



100
101
102
# File 'lib/tina4/websocket.rb', line 100

def room_count(room_name)
  (@rooms[room_name] || Set.new).size
end

#upgrade?(env) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/tina4/websocket.rb', line 28

def upgrade?(env)
  upgrade = env["HTTP_UPGRADE"] || ""
  upgrade.downcase == "websocket"
end