Class: Startback::Websocket::Hub::Room
- Defined in:
- lib/startback/websocket/hub/room.rb
Constant Summary
Constants inherited from App
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#participants ⇒ Object
readonly
Returns the value of attribute participants.
Instance Method Summary collapse
- #add(participant) ⇒ Object
- #broadcast(message) ⇒ Object
- #include?(participant) ⇒ Boolean
-
#initialize(name) ⇒ Room
constructor
A new instance of Room.
- #remove(participant) ⇒ Object
Methods inherited from App
Methods inherited from App
#call, #on_close, #on_error, #on_message, #on_open
Constructor Details
#initialize(name) ⇒ Room
Returns a new instance of Room.
7 8 9 10 |
# File 'lib/startback/websocket/hub/room.rb', line 7 def initialize(name) @name = name @participants = [] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/startback/websocket/hub/room.rb', line 11 def name @name end |
#participants ⇒ Object (readonly)
Returns the value of attribute participants.
11 12 13 |
# File 'lib/startback/websocket/hub/room.rb', line 11 def participants @participants end |
Instance Method Details
#add(participant) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/startback/websocket/hub/room.rb', line 13 def add(participant) raise "Participant instance expected" unless participant.is_a? Participant @participants << participant participant.socket.on :close do |event| remove(participant) end end |
#broadcast(message) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/startback/websocket/hub/room.rb', line 31 def broadcast() puts "Broadcasting to #{@participants.size} participants" @participants.each do |p| p.socket.send({ headers: { room: @name, }, body: }.to_json) end end |
#include?(participant) ⇒ Boolean
26 27 28 29 |
# File 'lib/startback/websocket/hub/room.rb', line 26 def include?(participant) raise "Participant instance expected" unless participant.is_a? Participant @participants.include? participant end |
#remove(participant) ⇒ Object
21 22 23 24 |
# File 'lib/startback/websocket/hub/room.rb', line 21 def remove(participant) raise "Participant instance expected" unless participant.is_a? Participant @participants.delete participant end |