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.



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/tina4/websocket.rb', line 234

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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



231
232
233
# File 'lib/tina4/websocket.rb', line 231

def id
  @id
end

#on_close_handlerObject

Returns the value of attribute on_close_handler.



232
233
234
# File 'lib/tina4/websocket.rb', line 232

def on_close_handler
  @on_close_handler
end

#on_error_handlerObject

Returns the value of attribute on_error_handler.



232
233
234
# File 'lib/tina4/websocket.rb', line 232

def on_error_handler
  @on_error_handler
end

#on_message_handlerObject

Returns the value of attribute on_message_handler.



232
233
234
# File 'lib/tina4/websocket.rb', line 232

def on_message_handler
  @on_message_handler
end

#paramsObject

Returns the value of attribute params.



232
233
234
# File 'lib/tina4/websocket.rb', line 232

def params
  @params
end

#pathObject

Returns the value of attribute path.



232
233
234
# File 'lib/tina4/websocket.rb', line 232

def path
  @path
end

#roomsObject (readonly)

Returns the value of attribute rooms.



231
232
233
# File 'lib/tina4/websocket.rb', line 231

def rooms
  @rooms
end

Instance Method Details

#broadcast(message, include_self: false) ⇒ Object

Broadcast a message to all other connections on the same path



274
275
276
277
278
279
280
281
282
# File 'lib/tina4/websocket.rb', line 274

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



266
267
268
269
270
271
# File 'lib/tina4/websocket.rb', line 266

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

#build_frame(opcode, data) ⇒ Object



343
344
345
# File 'lib/tina4/websocket.rb', line 343

def build_frame(opcode, data)
  Tina4.build_frame(opcode, data)
end

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



307
308
309
310
311
312
# File 'lib/tina4/websocket.rb', line 307

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



256
257
258
259
# File 'lib/tina4/websocket.rb', line 256

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

#leave_room(room_name) ⇒ Object



261
262
263
264
# File 'lib/tina4/websocket.rb', line 261

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

#on_close(&block) ⇒ Object

Register a close handler (decorator style, matching Python).



252
253
254
# File 'lib/tina4/websocket.rb', line 252

def on_close(&block)
  @on_close_handler = block
end

#on_message(&block) ⇒ Object

Register a message handler (decorator style, matching Python).



247
248
249
# File 'lib/tina4/websocket.rb', line 247

def on_message(&block)
  @on_message_handler = block
end

#read_frameObject



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/tina4/websocket.rb', line 314

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



284
285
286
287
288
289
290
# File 'lib/tina4/websocket.rb', line 284

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

#send_json(data) ⇒ Object

Serialize a Hash/Array (or any JSON-coercible value) and send as a text frame. Matches Python/PHP send_json.



296
297
298
# File 'lib/tina4/websocket.rb', line 296

def send_json(data)
  send_text(JSON.generate(data))
end

#send_pong(data) ⇒ Object



300
301
302
303
304
305
# File 'lib/tina4/websocket.rb', line 300

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