Module: Roda::RodaPlugins::Websockets::RequestMethods

Defined in:
lib/roda/plugins/websockets.rb

Instance Method Summary collapse

Instance Method Details

#websocket(opts = OPTS) ⇒ Object

If the request is a websocket request, yield a websocket to the block, and return the appropriate rack response after the block returns. opts is an options hash used when creating the websocket, except the following options are handled specially:

:protocols

Set the protocols to accept, should be an array of strings.



90
91
92
93
94
95
96
97
98
99
# File 'lib/roda/plugins/websockets.rb', line 90

def websocket(opts=OPTS)
  if websocket?
    always do
      opts = Hash[roda_class.opts[:websockets_opts]].merge!(opts)
      ws = WebSocket.new(env, opts.delete(:protocols), opts)
      yield ws
      halt ws.rack_response
    end
  end
end

#websocket?Boolean

True if this request is a websocket request, false otherwise.

Returns:

  • (Boolean)


79
80
81
# File 'lib/roda/plugins/websockets.rb', line 79

def websocket?
  WebSocket.websocket?(env)
end