Class: Async::Cable::Middleware
- Inherits:
-
Object
- Object
- Async::Cable::Middleware
- Defined in:
- lib/async/cable/middleware.rb
Overview
Rack middleware that intercepts WebSocket upgrade requests and dispatches them to ActionCable, passing all other requests to the next app in the middleware stack.
Instance Attribute Summary collapse
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
-
#call(env) ⇒ Object
Handle an incoming Rack request.
-
#initialize(app, path: "/cable", server: ActionCable.server) ⇒ Middleware
constructor
Create a new middleware instance.
-
#valid_path?(env) ⇒ Boolean
Check whether the request path matches the configured cable path.
Constructor Details
#initialize(app, path: "/cable", server: ActionCable.server) ⇒ Middleware
Create a new middleware instance.
19 20 21 22 23 24 25 |
# File 'lib/async/cable/middleware.rb', line 19 def initialize(app, path: "/cable", server: ActionCable.server) @app = app @path = path @server = server @coder = ActiveSupport::JSON @protocols = ::ActionCable::INTERNAL[:protocols] end |
Instance Attribute Details
#server ⇒ Object (readonly)
Returns the value of attribute server.
27 28 29 |
# File 'lib/async/cable/middleware.rb', line 27 def server @server end |
Instance Method Details
#call(env) ⇒ Object
Handle an incoming Rack request. WebSocket upgrade requests on the configured path are handed off to ActionCable; all other requests are forwarded to the next app in the middleware stack.
39 40 41 42 43 44 45 46 47 |
# File 'lib/async/cable/middleware.rb', line 39 def call(env) if valid_path?(env) and Async::WebSocket::Adapters::Rack.websocket?(env) and allow_request_origin?(env) Async::WebSocket::Adapters::Rack.open(env, protocols: @protocols) do |websocket| handle_incoming_websocket(env, websocket) end else @app.call(env) end end |
#valid_path?(env) ⇒ Boolean
Check whether the request path matches the configured cable path.
32 33 34 |
# File 'lib/async/cable/middleware.rb', line 32 def valid_path?(env) env["PATH_INFO"] == @path end |