-
-
Notifications
You must be signed in to change notification settings - Fork 801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updated websocket consumers for ASGI v2.3. #2002
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,11 +44,15 @@ def websocket_connect(self, message): | |
def connect(self): | ||
self.accept() | ||
|
||
def accept(self, subprotocol=None): | ||
def accept(self, subprotocol=None, headers=None): | ||
""" | ||
Accepts an incoming socket | ||
""" | ||
super().send({"type": "websocket.accept", "subprotocol": subprotocol}) | ||
message = {"type": "websocket.accept", "subprotocol": subprotocol} | ||
if headers: | ||
message["headers"] = list(headers) | ||
|
||
super().send(message) | ||
|
||
def websocket_receive(self, message): | ||
""" | ||
|
@@ -79,14 +83,16 @@ def send(self, text_data=None, bytes_data=None, close=False): | |
if close: | ||
self.close(close) | ||
|
||
def close(self, code=None): | ||
def close(self, code=None, reason=None): | ||
""" | ||
Closes the WebSocket from the server end | ||
""" | ||
message = {"type": "websocket.close"} | ||
if code is not None and code is not True: | ||
super().send({"type": "websocket.close", "code": code}) | ||
else: | ||
super().send({"type": "websocket.close"}) | ||
message["code"] = code | ||
if reason: | ||
message["reason"] = reason | ||
Comment on lines
+93
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if those conditionals are needed, but should be fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea is to not pollute the response with an empty "reason" which is optional in any case. |
||
super().send(message) | ||
|
||
def websocket_disconnect(self, message): | ||
""" | ||
|
@@ -179,11 +185,14 @@ async def websocket_connect(self, message): | |
async def connect(self): | ||
await self.accept() | ||
|
||
async def accept(self, subprotocol=None): | ||
async def accept(self, subprotocol=None, headers=None): | ||
""" | ||
Accepts an incoming socket | ||
""" | ||
await super().send({"type": "websocket.accept", "subprotocol": subprotocol}) | ||
message = {"type": "websocket.accept", "subprotocol": subprotocol} | ||
if headers: | ||
message["headers"] = list(headers) | ||
await super().send(message) | ||
|
||
async def websocket_receive(self, message): | ||
""" | ||
|
@@ -214,14 +223,16 @@ async def send(self, text_data=None, bytes_data=None, close=False): | |
if close: | ||
await self.close(close) | ||
|
||
async def close(self, code=None): | ||
async def close(self, code=None, reason=None): | ||
""" | ||
Closes the WebSocket from the server end | ||
""" | ||
message = {"type": "websocket.close"} | ||
if code is not None and code is not True: | ||
await super().send({"type": "websocket.close", "code": code}) | ||
else: | ||
await super().send({"type": "websocket.close"}) | ||
message["code"] = code | ||
if reason: | ||
message["reason"] = reason | ||
await super().send(message) | ||
|
||
async def websocket_disconnect(self, message): | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my curiosity, why is there a check for the "code not being True"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, hard questions! I think this is a compatibility check. It's been a while, but IIRC, there was a case where a "True" value was being passed to close... I need to look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, this is not my code. Phew. So, your guess is as good as mine. Maybe in the distant past, someone would call
close(True)
for whatever reason and this is trying to allow for that? Nothing in the library or unit tests which is doing that, though.