Module: SolanaRuby::WebSocketHandlers

Included in:
WebSocketClient
Defined in:
lib/solana_ruby/web_socket_handlers.rb

Instance Method Summary collapse

Instance Method Details

#setup_handlers(ws, client) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/solana_ruby/web_socket_handlers.rb', line 5

def setup_handlers(ws, client)
  ws.on :message do |msg|
    begin
      data = JSON.parse(msg.data)
      if data['error']
        puts "Error: #{data['error']['message']} with the code #{data['error']['code']}"
      else
        client.handle_message(data)
      end
    rescue JSON::ParserError => e
      puts "Failed to parse message: #{e.message}"
    end
  end

  ws.on :open do
    puts 'Web Socket connection established.'
  end

  ws.on :close do |e|
    puts "Web Socket connection closed: #{e.inspect}"
    client.attempt_reconnect if client.instance_variable_get(:@auto_reconnect)
  end

  ws.on :error do |e|
    puts "Error: #{e.inspect}"
    client.attempt_reconnect if client.instance_variable_get(:@auto_reconnect)
  end
end