Module: Padrino::WebSockets::SpiderGazelle::Routing

Defined in:
lib/padrino-websockets/spider-gazelle/routing.rb

Instance Method Summary collapse

Instance Method Details

#websocket(channel, *args, &block) ⇒ Object Also known as: ws

Creates a WebSocket endpoint using SpiderGazelle + libuv.

It handles upgrading the HTTP connection for you. You can nest this inside controllers as you would do with regular actions in Padrino.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/padrino-websockets/spider-gazelle/routing.rb', line 13

def websocket(channel, *args, &block)
  get channel, *args do
    # Let some other action try to handle the request if it's not a WebSocket.
    throw :pass unless request.env['rack.hijack']

    event_context = self
    ws_channel = params[:channel] || channel

    # It's a WebSocket. Get the libuv promise and manage its events
    request.env['rack.hijack'].call.then do |hijacked|
      ws = ::SpiderGazelle::Websocket.new hijacked.socket, hijacked.env

      set_websocket_user

      Padrino::WebSockets::SpiderGazelle::EventManager.new(
        ws_channel, session['websocket_user'], ws, event_context, &block)
      ws.start
    end
  end
end