class Rookout::ComWs::Pinger

Public Class Methods

new(connection, interval, timeout) click to toggle source
# File lib/rookout/com_ws/pinger.rb, line 6
def initialize connection, interval, timeout
  @interval = interval
  @timeout = timeout
  @connection = connection

  @last_pong = Time.now
  @last_ping = Time.now
end

Public Instance Methods

repeat() { || ... } click to toggle source
# File lib/rookout/com_ws/pinger.rb, line 15
def repeat
  loop do
    if Time.now - @last_ping > @interval
      Logger.instance.debug "Sending Ping"
      @last_ping = Time.now
      begin
        @connection.ping Time.now.to_s do
          Logger.instance.debug "Got Ping reply"
          @last_pong = Time.now
        end
      rescue RuntimeError, Errno::EPIPE
        Logger.instance.debug "Failed to send ping"
        break
      end
    end

    if Time.now - @last_pong > @timeout
      Logger.instance.debug "Ping timeout"
      break
    end

    yield
  end
end