Class: Async::WebSocket::Server

Inherits:
Protocol::HTTP::Middleware
  • Object
show all
Includes:
Protocol::WebSocket::Headers
Defined in:
lib/async/websocket/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(delegate, protocols: [], handler: Connection) ⇒ Server



16
17
18
19
20
21
# File 'lib/async/websocket/server.rb', line 16

def initialize(delegate, protocols: [], handler: Connection)
  super(delegate)
  
  @protocols = protocols
  @handler = handler
end

Instance Method Details

#call(request) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/async/websocket/server.rb', line 32

def call(request)
  if request.protocol == PROTOCOL
    # Select websocket sub-protocol:
    protocol = select_protocol(request)
    
    # request.headers = nil
    
    Response.for(request, headers, protocol: protocol, **options) do |stream|
      framer = Protocol::WebSocket::Framer.new(stream)
      
      yield handler.call(framer, protocol)
    end
  else
    super
  end
end

#response(request) ⇒ Object



29
30
# File 'lib/async/websocket/server.rb', line 29

def response(request)
end

#select_protocol(request) ⇒ Object



23
24
25
26
27
# File 'lib/async/websocket/server.rb', line 23

def select_protocol(request)
  if requested_protocol = request.headers[SEC_WEBSOCKET_PROTOCOL]
    return (requested_protocol & @protocols).first
  end
end