aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_cable/connection/heartbeat.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/action_cable/connection/heartbeat.rb')
-rw-r--r--lib/action_cable/connection/heartbeat.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/action_cable/connection/heartbeat.rb b/lib/action_cable/connection/heartbeat.rb
new file mode 100644
index 0000000000..47cd937c25
--- /dev/null
+++ b/lib/action_cable/connection/heartbeat.rb
@@ -0,0 +1,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 \ No newline at end of file