Module: Async::WebSocket::Adapters::HTTP

Includes:
Protocol::WebSocket::Headers
Defined in:
lib/async/websocket/adapters/http.rb

Class Method Summary collapse

Class Method Details

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/async/websocket/adapters/http.rb', line 36

def self.open(request, headers: [], protocols: [], handler: Connection, **options, &block)
  if websocket?(request)
    # Select websocket sub-protocol:
    if requested_protocol = request.headers[SEC_WEBSOCKET_PROTOCOL]
      protocol = (requested_protocol & protocols).first
    end
    
    response = Response.for(request, headers, protocol: protocol, **options) do |stream|
      # Once we get to this point, we no longer need to hold on to the response:
      response = nil
      
      framer = Protocol::WebSocket::Framer.new(stream)
      connection = handler.call(framer, protocol)
      
      yield connection
      
      connection.close unless connection.closed?
    end
    
    # Once we get to this point, we no longer need to hold on to the request:
    request = nil
    
    return response
  end
end

.websocket?(request) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/async/websocket/adapters/http.rb', line 32

def self.websocket?(request)
  Array(request.protocol).any? { |e| e.casecmp?(PROTOCOL) }
end