Class: Volt::SocketConnectionHandler

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, *args) ⇒ SocketConnectionHandler

Returns a new instance of SocketConnectionHandler.



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

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

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

Instance Attribute Details

#user_idObject

We track the connected user_id with the channel for use with permissions. This may be changed as new listeners connect, which is fine.



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

def user_id
  @user_id
end

Class Method Details

.dispatcherObject



16
17
18
# File 'lib/volt/server/socket_connection_handler.rb', line 16

def self.dispatcher
  @@dispatcher
end

.dispatcher=(val) ⇒ Object



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

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



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

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

Instance Method Details

#closedObject



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/volt/server/socket_connection_handler.rb', line 52

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

    QueryTasks.new(self).close!
  else
    Volt.logger.error("Socket Error: Connection already closed\n#{inspect}")
  end
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



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

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



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

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

  @session.send(str)
end