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, extensions: ::Protocol::WebSocket::Extensions::Server.default, **options, &block) ⇒ Object



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

def self.open(request, headers: [], protocols: [], handler: Connection, extensions: ::Protocol::WebSocket::Extensions::Server.default, **options, &block)
	if websocket?(request)
		headers = Protocol::HTTP::Headers[headers]
		
		# Select websocket sub-protocol:
		if requested_protocol = request.headers[SEC_WEBSOCKET_PROTOCOL]
			protocol = (requested_protocol & protocols).first
		end
		
		if extensions and extension_headers = request.headers[SEC_WEBSOCKET_EXTENSIONS]
			extensions.accept(extension_headers) do |header|
				headers.add(SEC_WEBSOCKET_EXTENSIONS, header.join(";"))
			end
		end
							
		response = Response.for(request, headers, protocol: protocol, **options) do |stream|
			framer = Protocol::WebSocket::Framer.new(stream)
			connection = handler.call(framer, protocol, extensions)
			
			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)


34
35
36
# File 'lib/async/websocket/adapters/http.rb', line 34

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