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

Returns a new instance of Server.



31
32
33
34
35
36
# File 'lib/async/websocket/server.rb', line 31

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

Instance Method Details

#call(request) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/async/websocket/server.rb', line 47

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



44
45
# File 'lib/async/websocket/server.rb', line 44

def response(request)
end

#select_protocol(request) ⇒ Object



38
39
40
41
42
# File 'lib/async/websocket/server.rb', line 38

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