Class: Jebanni::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/jebanni/channel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#connectionsObject (readonly)

Returns the value of attribute connections.



3
4
5
# File 'lib/jebanni/channel.rb', line 3

def connections
  @connections
end

#historyObject (readonly)

Returns the value of attribute history.



3
4
5
# File 'lib/jebanni/channel.rb', line 3

def history
  @history
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/jebanni/channel.rb', line 3

def id
  @id
end

#serverObject (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