Class: LiteCable::Server::HeartBeat
- Inherits:
-
Object
- Object
- LiteCable::Server::HeartBeat
- Defined in:
- lib/lite_cable/server/heart_beat.rb
Overview
Sends pings to sockets
Constant Summary collapse
- BEAT_INTERVAL =
3
Instance Method Summary collapse
- #add(socket) ⇒ Object
-
#initialize ⇒ HeartBeat
constructor
A new instance of HeartBeat.
- #remove(socket) ⇒ Object
-
#run ⇒ Object
rubocop: disable Metrics/MethodLength.
- #stop ⇒ Object
Constructor Details
#initialize ⇒ HeartBeat
Returns a new instance of HeartBeat.
9 10 11 12 |
# File 'lib/lite_cable/server/heart_beat.rb', line 9 def initialize @sockets = [] run end |
Instance Method Details
#add(socket) ⇒ Object
14 15 16 |
# File 'lib/lite_cable/server/heart_beat.rb', line 14 def add(socket) @sockets << socket end |
#remove(socket) ⇒ Object
18 19 20 |
# File 'lib/lite_cable/server/heart_beat.rb', line 18 def remove(socket) @sockets.delete(socket) end |
#run ⇒ Object
rubocop: disable Metrics/MethodLength
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/lite_cable/server/heart_beat.rb', line 27 def run Thread.new do Thread.current.abort_on_exception = true loop do break if @stopped unless @sockets.empty? msg = Time.now.to_i @sockets.each do |socket| socket.transmit msg end end sleep BEAT_INTERVAL end end end |
#stop ⇒ Object
22 23 24 |
# File 'lib/lite_cable/server/heart_beat.rb', line 22 def stop @stopped = true end |