Class: ChannelHandler

Inherits:
SockJS::Session
  • Object
show all
Defined in:
lib/volt/server/channel_handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, *args) ⇒ ChannelHandler

Returns a new instance of ChannelHandler.



22
23
24
25
26
27
28
29
# File 'lib/volt/server/channel_handler.rb', line 22

def initialize(session, *args)
  @session = session

  @@channels ||= []
  @@channels << self
  
  super
end

Class Method Details

.dispatcher=(val) ⇒ Object

Create one instance of the dispatcher



7
8
9
# File 'lib/volt/server/channel_handler.rb', line 7

def self.dispatcher=(val)
  @@dispatcher = val
end

.send_message_all(skip_channel = nil, *args) ⇒ Object

Sends a message to all, optionally skipping a users channel



12
13
14
15
16
17
18
19
20
# File 'lib/volt/server/channel_handler.rb', line 12

def self.send_message_all(skip_channel=nil, *args)
  @@channels.each do |channel|
    if skip_channel && channel == skip_channel
      next
    end
    channel.send_message(*args)
  end
  
end

Instance Method Details

#closedObject



46
47
48
49
# File 'lib/volt/server/channel_handler.rb', line 46

def closed    
  # Remove ourself from the available channels
  @@channels.delete(self)
end

#process_message(message) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/volt/server/channel_handler.rb', line 31

def process_message(message)
  # self.class.message_all(message)
  # Messages are json and wrapped in an array
  message = JSON.parse(message).first
  
  puts "GOT: #{message.inspect}"
  @@dispatcher.dispatch(self, message)
end

#send_message(*args) ⇒ Object



40
41
42
43
44
# File 'lib/volt/server/channel_handler.rb', line 40

def send_message(*args)
  str = JSON.dump([*args])
  
  send(str)
end