Skip to content

Commit

Permalink
Ŝelect websocket subprotocol; Issue #168
Browse files Browse the repository at this point in the history
  • Loading branch information
spaceone committed Jan 20, 2017
1 parent 4b1619a commit 0947a45
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions circuits/web/websockets/dispatcher.py
Expand Up @@ -57,6 +57,7 @@ def _on_request(self, event, request, response):
self._protocol_version = 13
headers = request.headers
sec_key = headers.get("Sec-WebSocket-Key", "").encode("utf-8")
subprotocols = headers.elements("Sec-WebSocket-Protocol")

connection_tokens = [s.strip() for s in
headers.get("Connection", "").lower().split(",")]
Expand Down Expand Up @@ -88,13 +89,18 @@ def _on_request(self, event, request, response):
response.headers["Upgrade"] = "WebSocket"
response.headers["Connection"] = "Upgrade"
response.headers["Sec-WebSocket-Accept"] = accept.decode()
if subprotocols:
response.headers["Sec-WebSocket-Protocol"] = self.select_subprotocol(subprotocols)
codec = WebSocketCodec(request.sock, channel=self._wschannel)
self._codecs[request.sock] = codec
codec.register(self)
return response
finally:
event.stop()

def select_subprotocol(self, subprotocols):
return subprotocols[0]

@handler("response_complete")
def _on_response_complete(self, e, value):
response = e.args[0]
Expand Down

0 comments on commit 0947a45

Please sign in to comment.