Class: WebSocketClient
- Inherits:
-
Object
- Object
- WebSocketClient
- Defined in:
- lib/javonet-ruby-sdk/core/web_socket_client/web_socket_client.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/Linux/X64/core/web_socket_client/web_socket_client.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/MacOs/X64/core/web_socket_client/web_socket_client.rb,
lib/javonet-ruby-sdk/Binaries/Ruby/Windows/X64/core/web_socket_client/web_socket_client.rb
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(url) ⇒ WebSocketClient
constructor
A new instance of WebSocketClient.
- #open? ⇒ Boolean
- #receive_message(timeout: 5) ⇒ Object
- #send_message(message) ⇒ Object
Constructor Details
#initialize(url) ⇒ WebSocketClient
Returns a new instance of WebSocketClient.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/javonet-ruby-sdk/core/web_socket_client/web_socket_client.rb', line 5 def initialize(url) @uri = URI.parse(url) raise "Only ws:// or wss:// URLs are supported" unless %w[ws wss].include?(@uri.scheme) @host = @uri.host @port = @uri.port || default_port @path = @uri.path.empty? ? '/' : @uri.path @path += "?#{@uri.query}" if @uri.query @socket = open_socket @handshake = WebSocket::Handshake::Client.new( url: url ) perform_handshake end |
Class Method Details
.add_or_get_client(url) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/javonet-ruby-sdk/core/web_socket_client/web_socket_client.rb', line 47 def self.add_or_get_client(url) @@clients ||= {} client = @@clients[url] if client.nil? || !client.open? client&.close client = new(url) @@clients[url] = client end client end |
.send_message(url, message) ⇒ Object
58 59 60 61 62 |
# File 'lib/javonet-ruby-sdk/core/web_socket_client/web_socket_client.rb', line 58 def self.(url, ) client = add_or_get_client(url) client.() client. end |
Instance Method Details
#close ⇒ Object
68 69 70 71 72 |
# File 'lib/javonet-ruby-sdk/core/web_socket_client/web_socket_client.rb', line 68 def close frame = WebSocket::Frame::Outgoing::Client.new(version: @handshake.version, type: :close) @socket.write(frame.to_s) @socket.close end |
#open? ⇒ Boolean
64 65 66 |
# File 'lib/javonet-ruby-sdk/core/web_socket_client/web_socket_client.rb', line 64 def open? !@socket.closed? end |
#receive_message(timeout: 5) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/javonet-ruby-sdk/core/web_socket_client/web_socket_client.rb', line 27 def (timeout: 5) buffer = '' start_time = Time.now while Time.now - start_time < timeout if IO.select([@socket], nil, nil, 0.1) chunk = @socket.read_nonblock(1024, exception: false) break if chunk.nil? buffer << chunk frame = WebSocket::Frame::Incoming::Client.new(version: @handshake.version) frame << buffer data = frame.next return data.data.bytes if data end end nil end |
#send_message(message) ⇒ Object
22 23 24 25 |
# File 'lib/javonet-ruby-sdk/core/web_socket_client/web_socket_client.rb', line 22 def () frame = WebSocket::Frame::Outgoing::Client.new(version: @handshake.version, data: .pack("C*"), type: :binary) @socket.write(frame.to_s) end |