Class: Startback::Websocket::Hub::Room

Inherits:
App
  • Object
show all
Defined in:
lib/startback/websocket/hub/room.rb

Constant Summary

Constants inherited from App

App::JS_CLIENT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from App

#on_message, #room

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

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/startback/websocket/hub/room.rb', line 11

def name
  @name
end

#participantsObject (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(message)
  puts "Broadcasting to #{@participants.size} participants"
  @participants.each do |p|
    p.socket.send({
      headers: {
        room: @name,
      },
      body: message
    }.to_json)
  end
end

#include?(participant) ⇒ Boolean

Returns:

  • (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