2015-07-05 16:33:14 -04:00
module ActionCable
module Server
2015-07-07 17:13:00 -04:00
# Collection class for all the connections that's been established on this specific server. Remember, usually you'll run many cable servers, so
# you can't use this collection as an full list of all the connections established against your application. Use RemoteConnections for that.
# As such, this is primarily for internal use.
2015-07-05 16:33:14 -04:00
module Connections
2015-10-08 15:30:58 -04:00
BEAT_INTERVAL = 3
2015-07-05 16:33:14 -04:00
def connections
@connections || = [ ]
end
def add_connection ( connection )
connections << connection
end
def remove_connection ( connection )
connections . delete connection
end
2015-10-16 02:25:54 -04:00
# WebSocket connection implementations differ on when they'll mark a connection as stale. We basically never want a connection to go stale, as you
2015-10-08 15:30:58 -04:00
# then can't rely on being able to receive and send to it. So there's a 3 second heartbeat running on all connections. If the beat fails, we automatically
# disconnect.
def setup_heartbeat_timer
2015-10-08 17:58:20 -04:00
EM . next_tick do
@heartbeat_timer || = EventMachine . add_periodic_timer ( BEAT_INTERVAL ) do
2015-10-16 03:32:46 -04:00
EM . next_tick { connections . map ( & :beat ) }
2015-10-08 17:40:33 -04:00
end
2015-10-08 15:30:58 -04:00
end
end
2015-07-05 16:33:14 -04:00
def open_connections_statistics
connections . map ( & :statistics )
end
end
end
2015-10-16 02:25:54 -04:00
end