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.



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

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



24
25
26
# File 'lib/volt/server/socket_connection_handler.rb', line 24

def self.dispatcher
  @@dispatcher
end

.dispatcher=(val) ⇒ Object



20
21
22
# File 'lib/volt/server/socket_connection_handler.rb', line 20

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



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

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

Instance Method Details

#closedObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/volt/server/socket_connection_handler.rb', line 71

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

    begin
      @@dispatcher.close_channel(self)
    rescue DRb::DRbConnError => e
    # ignore drb read of @@dispatcher error if child has closed
    end
  else
    Volt.logger.error("Socket Error: Connection already closed\n#{inspect}")
  end
end

#inspectObject



87
88
89
# File 'lib/volt/server/socket_connection_handler.rb', line 87

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

#process_message(message) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/volt/server/socket_connection_handler.rb', line 37

def process_message(message)
  # Messages are json and wrapped in an array
  begin
    message = EJSON.parse(message).first
  rescue JSON::ParserError => e
    Volt.logger.error("Unable to process task request message: #{message.inspect}")
  end

  begin
    @@dispatcher.dispatch(self, message)
  rescue => e
    if defined?(DRb::DRbConnError) && e.is_a?(DRb::DRbConnError)
      # The child process was restarting, so drb failed to send
    else
      # re-raise the issue
      raise
    end
  end
end

#send_message(*args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/volt/server/socket_connection_handler.rb', line 57

def send_message(*args)
  str = EJSON.stringify([*args])

  @session.send(str)

  if RUNNING_SERVER == 'thin'
    # This might seem strange, but it prevents a delay with outgoing
    # messages.
    # TODO: Figure out the cause of the issue and submit a fix upstream.
    EM.next_tick {}
  end

end