Module: Async::WebSocket::Adapters::Native

Includes:
Protocol::WebSocket::Headers
Defined in:
lib/pakyow/realtime/websocket.rb

Class Method Summary collapse

Class Method Details

.open(request, headers: [], protocols: [], handler: Connection, **options) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pakyow/realtime/websocket.rb', line 140

def self.open(request, headers: [], protocols: [], handler: Connection, **options)
  if websocket?(request) && Array(request.protocol).include?(PROTOCOL)
    # Select websocket sub-protocol:
    if requested_protocol = request.headers[SEC_WEBSOCKET_PROTOCOL]
      protocol = (requested_protocol & protocols).first
    end

    Response.for(request, headers, protocol: protocol, **options) do |stream|
      framer = Protocol::WebSocket::Framer.new(stream)

      yield handler.call(framer, protocol)
    end
  else
    nil
  end
end

.websocket?(request) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/pakyow/realtime/websocket.rb', line 136

def self.websocket?(request)
  request.headers.include?("upgrade")
end