Class: Jebanni::Channel
- Inherits:
-
Object
- Object
- Jebanni::Channel
- Defined in:
- lib/jebanni/channel.rb
Instance Attribute Summary collapse
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
- #broadcast(data, event = nil) ⇒ Object
-
#initialize(id, server) ⇒ Channel
constructor
A new instance of Channel.
- #join(connection) ⇒ Object
- #leave(connection) ⇒ Object
- #refrain(since) ⇒ Object
Constructor Details
#initialize(id, server) ⇒ Channel
Returns a new instance of Channel.
5 6 7 8 9 10 11 |
# File 'lib/jebanni/channel.rb', line 5 def initialize(id, server) @id = id @history = [] @last_event_id = 0 @server = server @connections = [] end |
Instance Attribute Details
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
3 4 5 |
# File 'lib/jebanni/channel.rb', line 3 def connections @connections end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
3 4 5 |
# File 'lib/jebanni/channel.rb', line 3 def history @history end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/jebanni/channel.rb', line 3 def id @id end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
3 4 5 |
# File 'lib/jebanni/channel.rb', line 3 def server @server end |
Instance Method Details
#broadcast(data, event = nil) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/jebanni/channel.rb', line 28 def broadcast(data, event = nil) @last_event_id += 1 @history << {id: @last_event_id, event: event, data: data} #only keep the last 5000 Events if @history.size >= 6000 @history.slice!(0, @history.size - 1000) end server.broadcast_to(self, data, event, @last_event_id) end |
#join(connection) ⇒ Object
13 14 15 |
# File 'lib/jebanni/channel.rb', line 13 def join(connection) @connections << connection end |
#leave(connection) ⇒ Object
17 18 19 |
# File 'lib/jebanni/channel.rb', line 17 def leave(connection) @connections.delete connection end |
#refrain(since) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/jebanni/channel.rb', line 21 def refrain(since) @history.each do |history| next if history[:id] <= since server.broadcast_to(self, history[:data], history[:event], history[:id]) end end |