Class: SocketConnectionHandler

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, *args) ⇒ SocketConnectionHandler

Returns a new instance of SocketConnectionHandler.



26
27
28
29
30
31
32
33
# File 'lib/volt/server/socket_connection_handler.rb', line 26

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

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

  super
end

Class Method Details

.dispatcherObject



11
12
13
# File 'lib/volt/server/socket_connection_handler.rb', line 11

def self.dispatcher
  @@dispatcher
end

.dispatcher=(val) ⇒ Object

Create one instance of the dispatcher



7
8
9
# File 'lib/volt/server/socket_connection_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



16
17
18
19
20
21
22
23
24
# File 'lib/volt/server/socket_connection_handler.rb', line 16

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



53
54
55
56
57
58
# File 'lib/volt/server/socket_connection_handler.rb', line 53

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

  QueryTasks.new(self).close!
end

#inspectObject



60
61
62
# File 'lib/volt/server/socket_connection_handler.rb', line 60

def inspect
  "<#{self.class.to_s}:#{object_id}>"
end

#process_message(message) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/volt/server/socket_connection_handler.rb', line 35

def process_message(message)
  # self.class.message_all(message)
  # Messages are json and wrapped in an array
  message = JSON.parse(message).first

  @@dispatcher.dispatch(self, message)
end

#send_message(*args) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/volt/server/socket_connection_handler.rb', line 43

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

  begin
    send(str)
  rescue MetaState::WrongStateError => e
    puts "Tried to send to closed connection: #{e.inspect}"
  end
end