Class: ERPC::WebSocketConnection

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

Instance Method Summary collapse

Constructor Details

#initialize(url, timeout) ⇒ WebSocketConnection

Returns a new instance of WebSocketConnection.



18
19
20
21
22
23
# File 'lib/erpc_sdk/websocket.rb', line 18

def initialize(url, timeout)
  @uri = URI.parse(url)
  @timeout = timeout
  @write_mutex = Mutex.new
  connect
end

Instance Method Details

#closeObject



57
58
59
60
61
62
63
# File 'lib/erpc_sdk/websocket.rb', line 57

def close
  write_frame(0x8, "".b)
rescue StandardError
  nil
ensure
  @socket&.close
end

#read_messageObject



29
30
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
# File 'lib/erpc_sdk/websocket.rb', line 29

def read_message
  payload = +"".b
  started = false
  loop do
    final, opcode, chunk = read_frame
    case opcode
    when 0x0
      raise TransportError, "ERPC returned an invalid WebSocket frame" unless started
      payload << chunk
    when 0x1, 0x2
      raise TransportError, "ERPC returned an invalid WebSocket frame" if started
      started = true
      payload << chunk
    when 0x8
      raise TransportError, "ERPC WebSocket connection closed"
    when 0x9
      write_frame(0xA, chunk)
      next
    when 0xA
      next
    else
      raise TransportError, "ERPC returned an invalid WebSocket frame"
    end
    raise TransportError, "ERPC WebSocket message is too large" if payload.bytesize > MAX_MESSAGE_SIZE
    return payload.force_encoding(Encoding::UTF_8) if final
  end
end

#write_text(text) ⇒ Object



25
26
27
# File 'lib/erpc_sdk/websocket.rb', line 25

def write_text(text)
  write_frame(0x1, text.b)
end