Class: SolanaRuby::WebSocketClient
- Inherits:
-
Object
- Object
- SolanaRuby::WebSocketClient
- Includes:
- WebSocketHandlers, SolanaRuby::WebSocketMethods::AccountMethods, SolanaRuby::WebSocketMethods::LogMethods, SolanaRuby::WebSocketMethods::RootMethods, SolanaRuby::WebSocketMethods::SignatureMethods, SolanaRuby::WebSocketMethods::SlotMethods
- Defined in:
- lib/solana_ruby/web_socket_client.rb
Constant Summary
Constants included from SolanaRuby::WebSocketMethods::SignatureMethods
SolanaRuby::WebSocketMethods::SignatureMethods::BASE_64_ENCODING_OPTIONS, SolanaRuby::WebSocketMethods::SignatureMethods::FINALIZED_OPTIONS
Constants included from SolanaRuby::WebSocketMethods::AccountMethods
SolanaRuby::WebSocketMethods::AccountMethods::ENCODING_OPTIONS, SolanaRuby::WebSocketMethods::AccountMethods::FINALIZED_OPTIONS
Instance Attribute Summary collapse
-
#subscriptions ⇒ Object
readonly
Returns the value of attribute subscriptions.
Instance Method Summary collapse
- #attempt_reconnect ⇒ Object
- #connect ⇒ Object
- #handle_message(data) ⇒ Object
-
#initialize(url, auto_reconnect: true, reconnect_delay: 5) ⇒ WebSocketClient
constructor
A new instance of WebSocketClient.
- #reconnect ⇒ Object
- #subscribe(method, params = nil, &block) ⇒ Object
- #unsubscribe(method, subscription_id) ⇒ Object
Methods included from SolanaRuby::WebSocketMethods::SlotMethods
#on_slot_change, #remove_slot_change_listener
Methods included from SolanaRuby::WebSocketMethods::RootMethods
#on_root_change, #remove_root_listener
Methods included from SolanaRuby::WebSocketMethods::SignatureMethods
#on_signature, #on_signature_with_options, #remove_signature_listener
Methods included from SolanaRuby::WebSocketMethods::LogMethods
#on_logs, #on_logs_for_account, #on_logs_for_program, #remove_logs_listener
Methods included from SolanaRuby::WebSocketMethods::AccountMethods
#on_account_change, #on_program_account_change, #remove_account_change_listener, #remove_program_account_listener
Methods included from WebSocketHandlers
Constructor Details
#initialize(url, auto_reconnect: true, reconnect_delay: 5) ⇒ WebSocketClient
Returns a new instance of WebSocketClient.
19 20 21 22 23 24 25 26 27 |
# File 'lib/solana_ruby/web_socket_client.rb', line 19 def initialize(url, auto_reconnect: true, reconnect_delay: 5) @url = url @subscriptions = {} @auto_reconnect = auto_reconnect @reconnect_delay = reconnect_delay @connected = false @ws = nil connect end |
Instance Attribute Details
#subscriptions ⇒ Object (readonly)
Returns the value of attribute subscriptions.
17 18 19 |
# File 'lib/solana_ruby/web_socket_client.rb', line 17 def subscriptions @subscriptions end |
Instance Method Details
#attempt_reconnect ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/solana_ruby/web_socket_client.rb', line 84 def attempt_reconnect return unless @auto_reconnect puts "Attempting to reconnect in #{@reconnect_delay} seconds..." @connected = false sleep @reconnect_delay reconnect end |
#connect ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/solana_ruby/web_socket_client.rb', line 29 def connect return if @connected # Close existing WebSocket if it exists @ws&.close if @ws @ws = WebSocket::Client::Simple.connect(@url) setup_handlers(@ws, self) @connected = true rescue StandardError => e puts "Failed to connect: #{e.}" attempt_reconnect end |
#handle_message(data) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/solana_ruby/web_socket_client.rb', line 72 def (data) if data['id'] && @subscriptions[data['id']] @subscriptions[data['id']].call(data['result']) elsif data['method'] && data['params'] @subscriptions.each do |id, block| block.call(data['params']) if block end else puts "Unhandled message: #{data}" end end |
#reconnect ⇒ Object
43 44 45 46 |
# File 'lib/solana_ruby/web_socket_client.rb', line 43 def reconnect @connected = false connect end |
#subscribe(method, params = nil, &block) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/solana_ruby/web_socket_client.rb', line 48 def subscribe(method, params = nil, &block) id = SecureRandom.uuid @subscriptions[id] = block = { jsonrpc: '2.0', id: id, method: method } [:params] = params if params sleep 2 @ws.send(.to_json) id end |
#unsubscribe(method, subscription_id) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/solana_ruby/web_socket_client.rb', line 62 def unsubscribe(method, subscription_id) = { jsonrpc: '2.0', id: SecureRandom.uuid, method: method, params: [subscription_id] } @ws.send(.to_json) end |