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.



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

def initialize
  @connections = {}
  @handlers = {
    open: [],
    message: [],
    close: [],
    error: []
  }
end

Instance Attribute Details

#connectionsObject (readonly)

Returns the value of attribute connections.



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

def connections
  @connections
end

Instance Method Details

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



78
79
80
81
82
83
84
# File 'lib/tina4/websocket.rb', line 78

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

#handle_upgrade(env, socket) ⇒ Object



31
32
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
# File 'lib/tina4/websocket.rb', line 31

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)
      emit(:close, connection)
      socket.close rescue nil
    end
  end
end

#on(event, &block) ⇒ Object



22
23
24
# File 'lib/tina4/websocket.rb', line 22

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

#upgrade?(env) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/tina4/websocket.rb', line 26

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