Class: Solana::Ruby::Kit::RpcSubscriptions::Autopinger

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/solana/ruby/kit/rpc_subscriptions/autopinger.rb

Overview

Sends a WebSocket ping on a fixed interval to keep the connection alive. Mirrors @solana/rpc-subscriptions autopinger.

Instance Method Summary collapse

Constructor Details

#initialize(transport, interval: 5.0) ⇒ Autopinger

Returns a new instance of Autopinger.



12
13
14
15
16
# File 'lib/solana/ruby/kit/rpc_subscriptions/autopinger.rb', line 12

def initialize(transport, interval: 5.0)
  @transport = transport
  @interval  = interval
  @thread    = T.let(nil, T.nilable(Thread))
end

Instance Method Details

#startObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/solana/ruby/kit/rpc_subscriptions/autopinger.rb', line 19

def start
  @thread = Thread.new do
    loop do
      sleep(@interval)
      break if @transport.publisher.closed?

      begin
        @transport.instance_variable_get(:@ws)&.send(nil, type: :ping)
      rescue StandardError
        break
      end
    end
  end
  @thread.abort_on_exception = false
end

#stopObject



36
37
38
39
# File 'lib/solana/ruby/kit/rpc_subscriptions/autopinger.rb', line 36

def stop
  @thread&.kill
  @thread = nil
end