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.



553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/tina4/websocket.rb', line 553

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
  # Updated on every inbound frame; the idle reaper closes connections that
  # have been silent longer than TINA4_WS_IDLE_TIMEOUT (opt-in).
  @last_activity = Time.now.to_f
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



550
551
552
# File 'lib/tina4/websocket.rb', line 550

def id
  @id
end

#last_activityObject (readonly)

Returns the value of attribute last_activity.



550
551
552
# File 'lib/tina4/websocket.rb', line 550

def last_activity
  @last_activity
end

#on_close_handlerObject

Returns the value of attribute on_close_handler.



551
552
553
# File 'lib/tina4/websocket.rb', line 551

def on_close_handler
  @on_close_handler
end

#on_error_handlerObject

Returns the value of attribute on_error_handler.



551
552
553
# File 'lib/tina4/websocket.rb', line 551

def on_error_handler
  @on_error_handler
end

#on_message_handlerObject

Returns the value of attribute on_message_handler.



551
552
553
# File 'lib/tina4/websocket.rb', line 551

def on_message_handler
  @on_message_handler
end

#paramsObject

Returns the value of attribute params.



551
552
553
# File 'lib/tina4/websocket.rb', line 551

def params
  @params
end

#pathObject

Returns the value of attribute path.



551
552
553
# File 'lib/tina4/websocket.rb', line 551

def path
  @path
end

#roomsObject (readonly)

Returns the value of attribute rooms.



550
551
552
# File 'lib/tina4/websocket.rb', line 550

def rooms
  @rooms
end

Instance Method Details

#broadcast(message, include_self: false) ⇒ Object

Broadcast a message to all other connections on the same path



601
602
603
604
605
606
607
608
609
# File 'lib/tina4/websocket.rb', line 601

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



593
594
595
596
597
598
# File 'lib/tina4/websocket.rb', line 593

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



679
680
681
# File 'lib/tina4/websocket.rb', line 679

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

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



643
644
645
646
647
648
# File 'lib/tina4/websocket.rb', line 643

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

#closed?Boolean

True once a write has failed (broken pipe / closed socket). The manager’s resilient broadcast path uses this to prune dead connections.

Returns:

  • (Boolean)


613
614
615
# File 'lib/tina4/websocket.rb', line 613

def closed?
  @closed == true
end

#join_room(room_name) ⇒ Object



583
584
585
586
# File 'lib/tina4/websocket.rb', line 583

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

#leave_room(room_name) ⇒ Object



588
589
590
591
# File 'lib/tina4/websocket.rb', line 588

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).



579
580
581
# File 'lib/tina4/websocket.rb', line 579

def on_close(&block)
  @on_close_handler = block
end

#on_message(&block) ⇒ Object

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



574
575
576
# File 'lib/tina4/websocket.rb', line 574

def on_message(&block)
  @on_message_handler = block
end

#read_frameObject



650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# File 'lib/tina4/websocket.rb', line 650

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



617
618
619
620
621
622
623
624
625
626
# File 'lib/tina4/websocket.rb', line 617

def send(message)
  data = message.is_a?(String) ? message : message.to_s
  # Text frames must be valid UTF-8; binary payloads are sent verbatim.
  data = data.encode("UTF-8") if data.encoding != Encoding::ASCII_8BIT
  frame = build_frame(0x1, data)
  @socket.write(frame)
rescue IOError
  # Connection closed — mark dead so the broadcast path prunes it.
  @closed = true
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.



632
633
634
# File 'lib/tina4/websocket.rb', line 632

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

#send_pong(data) ⇒ Object



636
637
638
639
640
641
# File 'lib/tina4/websocket.rb', line 636

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

#touchObject

Mark inbound activity for the idle reaper.



569
570
571
# File 'lib/tina4/websocket.rb', line 569

def touch
  @last_activity = Time.now.to_f
end