Class: ActionCable::Server::Socket
- Inherits:
-
Object
- Object
- ActionCable::Server::Socket
- Defined in:
- lib/action_cable/server/socket.rb,
lib/action_cable/server/socket/stream.rb,
lib/action_cable/server/socket/web_socket.rb,
lib/action_cable/server/socket/client_socket.rb,
lib/action_cable/server/socket/message_buffer.rb
Overview
This class encapsulates all the low-level logic of working with the underlying WebSocket conenctions and delegate all the business-logic to the user-level connection object (e.g., ApplicationCable::Connection). This connection object is also responsible for handling encoding and decoding of messages, so the user-level connection object shouldn’t know about such details.
Defined Under Namespace
Classes: ClientSocket, MessageBuffer, Stream, WebSocket
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
-
#close ⇒ Object
Close the WebSocket connection.
-
#dispatch_websocket_message(websocket_message) ⇒ Object
:nodoc:.
-
#initialize(server, env, coder: ActiveSupport::JSON) ⇒ Socket
constructor
A new instance of Socket.
-
#inspect ⇒ Object
:nodoc:.
-
#on_close(reason, code) ⇒ Object
:nodoc:.
-
#on_error(message) ⇒ Object
:nodoc:.
-
#on_message(message) ⇒ Object
:nodoc:.
-
#on_open ⇒ Object
:nodoc:.
-
#perform_work(receiver, method, *args) ⇒ Object
Invoke a method on the connection asynchronously through the pool of thread workers.
-
#process ⇒ Object
Called by the server when a new WebSocket connection is established.
- #raw_transmit(message) ⇒ Object
-
#receive(websocket_message) ⇒ Object
Decodes WebSocket messages and dispatches them to subscribed channels.
-
#request ⇒ Object
The request that initiated the WebSocket connection is available here.
- #send_async(method, *arguments) ⇒ Object
-
#transmit(cable_message) ⇒ Object
Send a non-serialized message over the WebSocket connection.
Constructor Details
#initialize(server, env, coder: ActiveSupport::JSON) ⇒ Socket
Returns a new instance of Socket.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/action_cable/server/socket.rb', line 23 def initialize(server, env, coder: ActiveSupport::JSON) @server, @env, @coder = server, env, coder @worker_pool = server.worker_pool @logger = server.new_tagged_logger { request } @websocket = WebSocket.new(env, self, event_loop) @message_buffer = MessageBuffer.new(self) @protocol = nil @connection = config.connection_class.call.new(server, self) end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
18 19 20 |
# File 'lib/action_cable/server/socket.rb', line 18 def connection @connection end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
18 19 20 |
# File 'lib/action_cable/server/socket.rb', line 18 def env @env end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
18 19 20 |
# File 'lib/action_cable/server/socket.rb', line 18 def logger @logger end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
18 19 20 |
# File 'lib/action_cable/server/socket.rb', line 18 def protocol @protocol end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
18 19 20 |
# File 'lib/action_cable/server/socket.rb', line 18 def server @server end |
Instance Method Details
#close ⇒ Object
Close the WebSocket connection.
63 64 65 |
# File 'lib/action_cable/server/socket.rb', line 63 def close(...) websocket.close(...) if websocket.alive? end |
#dispatch_websocket_message(websocket_message) ⇒ Object
:nodoc:
90 91 92 93 94 95 96 97 98 |
# File 'lib/action_cable/server/socket.rb', line 90 def () # :nodoc: if websocket.alive? @connection.handle_incoming decode() else logger.error "Ignoring message processed after the WebSocket was closed: #{.inspect})" end rescue Exception => e logger.error "Could not handle incoming message: #{.inspect} [#{e.class} - #{e.}]: #{e.backtrace.first(5).join(" | ")}" end |
#inspect ⇒ Object
:nodoc:
120 121 122 |
# File 'lib/action_cable/server/socket.rb', line 120 def inspect # :nodoc: "#<#{self.class.name}:#{'%#016x' % (object_id << 1)}>" end |
#on_close(reason, code) ⇒ Object
:nodoc:
113 114 115 |
# File 'lib/action_cable/server/socket.rb', line 113 def on_close(reason, code) # :nodoc: send_async :handle_close end |
#on_error(message) ⇒ Object
:nodoc:
108 109 110 111 |
# File 'lib/action_cable/server/socket.rb', line 108 def on_error() # :nodoc: # log errors to make diagnosing socket errors easier logger.error "WebSocket error occurred: #{}" end |
#on_message(message) ⇒ Object
:nodoc:
104 105 106 |
# File 'lib/action_cable/server/socket.rb', line 104 def () # :nodoc: .append end |
#on_open ⇒ Object
:nodoc:
100 101 102 |
# File 'lib/action_cable/server/socket.rb', line 100 def on_open # :nodoc: send_async :handle_open end |
#perform_work(receiver, method, *args) ⇒ Object
Invoke a method on the connection asynchronously through the pool of thread workers.
68 69 70 |
# File 'lib/action_cable/server/socket.rb', line 68 def perform_work(receiver, method, *args) worker_pool.async_invoke(receiver, method, *args, connection: self) end |
#process ⇒ Object
Called by the server when a new WebSocket connection is established.
37 38 39 40 41 42 43 44 45 |
# File 'lib/action_cable/server/socket.rb', line 37 def process # :nodoc: logger.info if websocket.possible? && server.allow_request_origin?(env) respond_to_successful_request else respond_to_invalid_request end end |
#raw_transmit(message) ⇒ Object
56 57 58 59 60 |
# File 'lib/action_cable/server/socket.rb', line 56 def raw_transmit() return unless websocket.alive? websocket.transmit end |
#receive(websocket_message) ⇒ Object
Decodes WebSocket messages and dispatches them to subscribed channels. WebSocket message transfer encoding is always JSON.
86 87 88 |
# File 'lib/action_cable/server/socket.rb', line 86 def receive() # :nodoc: send_async :dispatch_websocket_message, end |
#request ⇒ Object
The request that initiated the WebSocket connection is available here. This gives access to the environment, cookies, etc.
77 78 79 80 81 82 |
# File 'lib/action_cable/server/socket.rb', line 77 def request @request ||= begin environment = Rails.application.env_config.merge(env) if defined?(Rails.application) && Rails.application ActionDispatch::Request.new(environment || env) end end |
#send_async(method, *arguments) ⇒ Object
72 73 74 |
# File 'lib/action_cable/server/socket.rb', line 72 def send_async(method, *arguments) worker_pool.async_invoke(self, method, *arguments) end |
#transmit(cable_message) ⇒ Object
Send a non-serialized message over the WebSocket connection.
50 51 52 53 54 |
# File 'lib/action_cable/server/socket.rb', line 50 def transmit() return unless websocket.alive? websocket.transmit encode() end |