Class: ERPC::WebSocketConnection
- Inherits:
-
Object
- Object
- ERPC::WebSocketConnection
- Defined in:
- lib/erpc_sdk/websocket.rb
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(url, timeout) ⇒ WebSocketConnection
constructor
A new instance of WebSocketConnection.
- #read_message ⇒ Object
- #write_text(text) ⇒ Object
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
#close ⇒ Object
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_message ⇒ Object
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 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 |