aboutsummaryrefslogtreecommitdiffstats
path: root/lib/action_cable
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2015-02-11 00:00:51 +0530
committerPratik Naik <pratiknaik@gmail.com>2015-02-11 00:00:51 +0530
commit00aec9c8e8b30cfb40454ed44693465843b0d4b2 (patch)
treefd3802a6fa6a0f4ae02b22ad555b6a50d6cffae5 /lib/action_cable
parent85ac703585a3bc413571e23d2e7dc3ca1e4cad2e (diff)
downloadrails-00aec9c8e8b30cfb40454ed44693465843b0d4b2.tar.gz
rails-00aec9c8e8b30cfb40454ed44693465843b0d4b2.tar.bz2
rails-00aec9c8e8b30cfb40454ed44693465843b0d4b2.zip
Ping the client every 3 seconds
Diffstat (limited to 'lib/action_cable')
-rw-r--r--lib/action_cable/server.rb14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/action_cable/server.rb b/lib/action_cable/server.rb
index e657f6d636..47c9352160 100644
--- a/lib/action_cable/server.rb
+++ b/lib/action_cable/server.rb
@@ -6,6 +6,8 @@ module ActionCable
class_attribute :worker_pool_size
self.worker_pool_size = 100
+ PING_INTERVAL = 3
+
class << self
def register_channels(*channel_classes)
self.registered_channels += channel_classes
@@ -30,6 +32,11 @@ module ActionCable
@websocket = Faye::WebSocket.new(@env)
+ @websocket.on(:open) do |event|
+ broadcast_ping_timestamp
+ @ping_timer = EventMachine.add_periodic_timer(PING_INTERVAL) { broadcast_ping_timestamp }
+ end
+
@websocket.on(:message) do |event|
message = event.data
worker_pool.async.invoke(self, :received_data, message) if message.is_a?(String)
@@ -38,6 +45,8 @@ module ActionCable
@websocket.on(:close) do |event|
worker_pool.async.invoke(self, :cleanup_subscriptions)
worker_pool.async.invoke(self, :disconnect) if respond_to?(:disconnect)
+
+ EventMachine.cancel_timer(@ping_timer) if @ping_timer
end
@websocket.rack_response
@@ -74,6 +83,10 @@ module ActionCable
end
private
+ def broadcast_ping_timestamp
+ broadcast({ identifier: '_ping', message: Time.now.to_i }.to_json)
+ end
+
def subscribe_channel(data)
id_key = data['identifier']
id_options = ActiveSupport::JSON.decode(id_key).with_indifferent_access
@@ -104,5 +117,6 @@ module ActionCable
def invalid_request
[404, {'Content-Type' => 'text/plain'}, ['Page not found']]
end
+
end
end