Class: Tina4::WebSocketConnection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, socket, ws_server: nil, path: "/") ⇒ WebSocketConnection

Returns a new instance of WebSocketConnection.



131
132
133
134
135
136
137
138
# File 'lib/tina4/websocket.rb', line 131

def initialize(id, socket, ws_server: nil, path: "/")
  @id = id
  @socket = socket
  @params = {}
  @ws_server = ws_server
  @path = path
  @rooms = Set.new
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



128
129
130
# File 'lib/tina4/websocket.rb', line 128

def id
  @id
end

#paramsObject

Returns the value of attribute params.



129
130
131
# File 'lib/tina4/websocket.rb', line 129

def params
  @params
end

#pathObject

Returns the value of attribute path.



129
130
131
# File 'lib/tina4/websocket.rb', line 129

def path
  @path
end

#roomsObject (readonly)

Returns the value of attribute rooms.



128
129
130
# File 'lib/tina4/websocket.rb', line 128

def rooms
  @rooms
end

Instance Method Details

#broadcast(message, include_self: false) ⇒ Object

Broadcast a message to all other connections on the same path



158
159
160
161
162
163
164
165
166
# File 'lib/tina4/websocket.rb', line 158

def broadcast(message, include_self: false)
  return unless @ws_server

  @ws_server.connections.each do |cid, conn|
    next if !include_self && cid == @id
    next if conn.path != @path
    conn.send_text(message)
  end
end

#broadcast_to_room(room_name, message, exclude_self: false) ⇒ Object



150
151
152
153
154
155
# File 'lib/tina4/websocket.rb', line 150

def broadcast_to_room(room_name, message, exclude_self: false)
  return unless @ws_server

  exclude = exclude_self ? @id : nil
  @ws_server.broadcast_to_room(room_name, message, exclude: exclude)
end

#close(code: 1000, reason: "") ⇒ Object



185
186
187
188
189
190
# File 'lib/tina4/websocket.rb', line 185

def close(code: 1000, reason: "")
  payload = [code].pack("n") + reason
  frame = build_frame(0x8, payload)
  @socket.write(frame) rescue nil
  @socket.close rescue nil
end

#join_room(room_name) ⇒ Object



140
141
142
143
# File 'lib/tina4/websocket.rb', line 140

def join_room(room_name)
  @rooms.add(room_name)
  @ws_server&.join_room_for(@id, room_name)
end

#leave_room(room_name) ⇒ Object



145
146
147
148
# File 'lib/tina4/websocket.rb', line 145

def leave_room(room_name)
  @rooms.delete(room_name)
  @ws_server&.leave_room_for(@id, room_name)
end

#read_frameObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/tina4/websocket.rb', line 192

def read_frame
  first_byte = @socket.getbyte
  return nil unless first_byte

  opcode = first_byte & 0x0F
  second_byte = @socket.getbyte
  return nil unless second_byte

  masked = (second_byte & 0x80) != 0
  length = second_byte & 0x7F

  if length == 126
    length = @socket.read(2).unpack1("n")
  elsif length == 127
    length = @socket.read(8).unpack1("Q>")
  end

  mask_key = masked ? @socket.read(4).bytes : nil
  data = @socket.read(length) || ""

  if masked && mask_key
    data = data.bytes.each_with_index.map { |b, i| b ^ mask_key[i % 4] }.pack("C*")
  end

  { opcode: opcode, data: data }
rescue IOError, EOFError
  nil
end

#send(message) ⇒ Object Also known as: send_text



168
169
170
171
172
173
174
# File 'lib/tina4/websocket.rb', line 168

def send(message)
  data = message.encode("UTF-8")
  frame = build_frame(0x1, data)
  @socket.write(frame)
rescue IOError
  # Connection closed
end

#send_pong(data) ⇒ Object



178
179
180
181
182
183
# File 'lib/tina4/websocket.rb', line 178

def send_pong(data)
  frame = build_frame(0xA, data || "")
  @socket.write(frame)
rescue IOError
  # Connection closed
end