Class: LiteCable::Server::SubscribersMap
- Inherits:
-
Object
- Object
- LiteCable::Server::SubscribersMap
- Defined in:
- lib/lite_cable/server/subscribers_map.rb
Overview
Instance Attribute Summary collapse
-
#sockets ⇒ Object
readonly
Returns the value of attribute sockets.
-
#streams ⇒ Object
readonly
Returns the value of attribute streams.
Instance Method Summary collapse
- #add_subscriber(stream, socket, channel) ⇒ Object
- #broadcast(stream, message, coder) ⇒ Object
-
#initialize ⇒ SubscribersMap
constructor
A new instance of SubscribersMap.
- #remove_socket(socket, channel) ⇒ Object
- #remove_subscriber(stream, socket, channel) ⇒ Object
Constructor Details
#initialize ⇒ SubscribersMap
Returns a new instance of SubscribersMap.
9 10 11 12 13 14 15 |
# File 'lib/lite_cable/server/subscribers_map.rb', line 9 def initialize @streams = Hash.new do |streams, stream_id| streams[stream_id] = Hash.new { |channels, channel_id| channels[channel_id] = [] } end @sockets = Hash.new { |h, k| h[k] = [] } @sync = Mutex.new end |
Instance Attribute Details
#sockets ⇒ Object (readonly)
Returns the value of attribute sockets.
7 8 9 |
# File 'lib/lite_cable/server/subscribers_map.rb', line 7 def sockets @sockets end |
#streams ⇒ Object (readonly)
Returns the value of attribute streams.
7 8 9 |
# File 'lib/lite_cable/server/subscribers_map.rb', line 7 def streams @streams end |
Instance Method Details
#add_subscriber(stream, socket, channel) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/lite_cable/server/subscribers_map.rb', line 17 def add_subscriber(stream, socket, channel) @sync.synchronize do @streams[stream][channel] << socket @sockets[socket] << [channel, stream] end end |
#broadcast(stream, message, coder) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/lite_cable/server/subscribers_map.rb', line 44 def broadcast(stream, , coder) list = @sync.synchronize do return unless @streams.key?(stream) @streams[stream].to_a end list.each do |(channel_id, sockets)| = (channel_id, , coder) sockets.each { |s| s.transmit } end end |
#remove_socket(socket, channel) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/lite_cable/server/subscribers_map.rb', line 32 def remove_socket(socket, channel) list = @sync.synchronize do return unless @sockets.key?(socket) @sockets[socket].dup end list.each do |(channel_id, stream)| remove_subscriber(stream, socket, channel) if channel == channel_id end end |
#remove_subscriber(stream, socket, channel) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/lite_cable/server/subscribers_map.rb', line 24 def remove_subscriber(stream, socket, channel) @sync.synchronize do @streams[stream][channel].delete(socket) @sockets[socket].delete([channel, stream]) cleanup stream, socket, channel end end |