Class: Volt::SocketConnectionHandler
- Defined in:
- lib/volt/server/socket_connection_handler.rb
Instance Attribute Summary collapse
-
#user_id ⇒ Object
We track the connected user_id with the channel for use with permissions.
Class Method Summary collapse
- .channels ⇒ Object
- .dispatcher ⇒ Object
- .dispatcher=(val) ⇒ Object
-
.send_message_all(skip_channel = nil, *args) ⇒ Object
Sends a message to all, optionally skipping a users channel.
Instance Method Summary collapse
- #closed ⇒ Object
-
#initialize(session, *args) ⇒ SocketConnectionHandler
constructor
A new instance of SocketConnectionHandler.
- #inspect ⇒ Object
- #process_message(message) ⇒ Object
- #send_message(*args) ⇒ Object
- #send_raw_message(str) ⇒ Object
-
#send_string_message(str) ⇒ Object
Used when the message is already encoded.
- #update_user_id(user_id) ⇒ Object
Constructor Details
#initialize(session, *args) ⇒ SocketConnectionHandler
Returns a new instance of SocketConnectionHandler.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/volt/server/socket_connection_handler.rb', line 13 def initialize(session, *args) @session = session @@channels ||= [] @@channels << self # Trigger a client connect event @@dispatcher.volt_app.trigger!("client_connect") end |
Instance Attribute Details
#user_id ⇒ Object
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
.channels ⇒ Object
51 52 53 |
# File 'lib/volt/server/socket_connection_handler.rb', line 51 def self.channels @@channels end |
.dispatcher ⇒ Object
47 48 49 |
# File 'lib/volt/server/socket_connection_handler.rb', line 47 def self.dispatcher defined?(@@dispatcher) ? @@dispatcher : nil end |
.dispatcher=(val) ⇒ Object
43 44 45 |
# File 'lib/volt/server/socket_connection_handler.rb', line 43 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
56 57 58 59 60 61 62 |
# File 'lib/volt/server/socket_connection_handler.rb', line 56 def self.(skip_channel = nil, *args) return unless defined?(@@channels) @@channels.each do |channel| next if skip_channel && channel == skip_channel channel.(*args) end end |
Instance Method Details
#closed ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/volt/server/socket_connection_handler.rb', line 107 def closed unless @closed @closed = true # Remove ourself from the available channels @@channels.delete(self) begin @@dispatcher.close_channel(self) # Check for volt_app (@@dispatcher could be an ErrorDispatcher) if @@dispatcher.respond_to?(:volt_app) # Trigger a client disconnect event @@dispatcher.volt_app.trigger!("client_disconnect") # Trigger a user disconnect event even if the user hasn't logged out if @user_id @@dispatcher.volt_app.trigger!("user_disconnect", @user_id) end end 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 |
#inspect ⇒ Object
135 136 137 |
# File 'lib/volt/server/socket_connection_handler.rb', line 135 def inspect "<#{self.class}:#{object_id}>" end |
#process_message(message) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/volt/server/socket_connection_handler.rb', line 64 def () # Messages are json and wrapped in an array begin = EJSON.parse().first rescue JSON::ParserError => e Volt.logger.error("Unable to process task request message: #{.inspect}") end begin @@dispatcher.dispatch(self, ) 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
89 90 91 92 93 94 |
# File 'lib/volt/server/socket_connection_handler.rb', line 89 def (*args) # Encode as EJSON str = EJSON.stringify([*args]) (str) end |
#send_raw_message(str) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/volt/server/socket_connection_handler.rb', line 96 def (str) @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 |
#send_string_message(str) ⇒ Object
Used when the message is already encoded
85 86 87 |
# File 'lib/volt/server/socket_connection_handler.rb', line 85 def (str) (str) end |
#update_user_id(user_id) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/volt/server/socket_connection_handler.rb', line 24 def update_user_id(user_id) if !@user_id && user_id # If there is currently no user id associated with this channel # and we get a new valid user_id, set it then trigger a # user_connect event @user_id = user_id @@dispatcher.volt_app.trigger!("user_connect", @user_id) elsif @user_id && !user_id # If there is currently a user id associated with this channel # and we get a nil user id, trigger a user_disconnect event then # set the id to nil @@dispatcher.volt_app.trigger!("user_disconnect", @user_id) @user_id = user_id else # Otherwise, lets just set the id (should never really run) @user_id = user_id end end |