Class: Volt::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.



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

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

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

  super
end

Class Method Details

.dispatcherObject



13
14
15
# File 'lib/volt/server/socket_connection_handler.rb', line 13

def self.dispatcher
  @@dispatcher
end

.dispatcher=(val) ⇒ Object

Create one instance of the dispatcher



9
10
11
# File 'lib/volt/server/socket_connection_handler.rb', line 9

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



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

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



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

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

  QueryTasks.new(self).close!
end

#inspectObject



64
65
66
# File 'lib/volt/server/socket_connection_handler.rb', line 64

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

#process_message(message) ⇒ Object



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

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



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

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

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

    # Mark this channel as closed
    closed
  end
end