Sha256: b82f78ffe8d8da26c356bd5d2809b4a561738fe8df8fc4ab7d719e90a6e10571

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

module LiteCable
  module Server
    # Sends pings to sockets
    class HeartBeat
      BEAT_INTERVAL = 3

      def initialize
        @sockets = []
        run
      end

      def add(socket)
        @sockets << socket
      end

      def remove(socket)
        @sockets.delete(socket)
      end

      def stop
        @stopped = true
      end

      # rubocop: disable Metrics/MethodLength
      def run
        Thread.new do
          Thread.current.abort_on_exception = true
          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 BEAT_INTERVAL
          end
        end
      end
      # rubocop: enable Metrics/MethodLength

      private

      def ping_message(time)
        { type: LiteCable::INTERNAL[:message_types][:ping], message: time }.to_json
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
litecable-0.5.0 lib/lite_cable/server/heart_beat.rb