class AnyCable::Rack::Pinger

Sends pings to sockets

Constants

INTERVAL

Attributes

coder[R]

Public Class Methods

new(coder) click to toggle source
# File lib/anycable/rack/pinger.rb, line 13
def initialize(coder)
  @coder = coder
  @_sockets = []
  @_stopped = false
end

Public Instance Methods

add(socket) click to toggle source
# File lib/anycable/rack/pinger.rb, line 19
def add(socket)
  @_sockets << socket
end
remove(socket) click to toggle source
# File lib/anycable/rack/pinger.rb, line 23
def remove(socket)
  @_sockets.delete(socket)
end
run() click to toggle source
# File lib/anycable/rack/pinger.rb, line 31
def run
  Thread.new do
    loop do
      break if @_stopped

      unless @_sockets.empty?
        msg = ping_message(Time.now.to_i)
        @_sockets.each do |socket|
          socket.transmit(msg)
        end
      end

      sleep(INTERVAL)
    end
  end
end
stop() click to toggle source
# File lib/anycable/rack/pinger.rb, line 27
def stop
  @_stopped = true
end

Private Instance Methods

ping_message(time) click to toggle source
# File lib/anycable/rack/pinger.rb, line 50
def ping_message(time)
  coder.encode({type: :ping, message: time})
end