module Padrino::WebSockets::SpiderGazelle::Routing

Public Instance Methods

websocket(channel, *args, &block) click to toggle source

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.

# 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
Also aliased as: ws
ws(channel, *args, &block)
Alias for: websocket