aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_cable/connection/heartbeat.rb
blob: 47cd937c2543e1cee043430749fea696c5f0b0b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
module ActionCable
  module Connection
    class Heartbeat
      BEAT_INTERVAL = 3
      
      def initialize(connection)
        @connection = connection
      end

      def start
        beat
        @timer = EventMachine.add_periodic_timer(BEAT_INTERVAL) { beat }
      end

      def stop
        EventMachine.cancel_timer(@timer) if @timer
      end

      private
        attr_reader :connection
        
        def beat
          connection.transmit({ identifier: '_ping', message: Time.now.to_i }.to_json)
        end
    end
  end
end