Module: Async::WebSocket::Adapters::Rack

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

Class Method Summary collapse

Class Method Details

.open(env, 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
61
62
# File 'lib/async/websocket/adapters/rack.rb', line 36

def self.open(env, headers: [], protocols: [], handler: Connection, **options, &block)
	if request = env['async.http.request'] and Array(request.protocol).include?(PROTOCOL)
		# 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|
			framer = Protocol::WebSocket::Framer.new(stream)
			
			connection = handler.call(framer, protocol)
			yield connection
			
			connection.close unless connection.closed?
		end
		
		headers = response.headers
		
		if protocol = response.protocol
			headers = Protocol::HTTP::Headers::Merged.new(headers, [
				['rack.protocol', protocol]
			])
		end
		
		return [response.status, headers, response.body]
	end
end

.websocket?(env) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.websocket?(env)
	request = env['async.http.request'] and Array(request.protocol).include?(PROTOCOL)
end