Skip to content

Add reason support for websocket.close - #1339

Closed
Chaostheorie wants to merge 3 commits into
Kludex:masterfrom
Chaostheorie:master
Closed

Chaostheorie wants to merge 3 commits into
Kludex:masterfrom
Chaostheorie:master

Conversation

@Chaostheorie

Copy link
Copy Markdown

Recreate old changes

Carbon-Copy of #992
Aims to resolve #991

@michallowasrzechonek-silvair

Copy link
Copy Markdown

Re "it's not part of the spec", django/asgiref@6469d0f

@Kludex Kludex left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some approaches we can take with the "reason". On the "code" key, we choose to send 1000 by default, but that's not quite what is written in the specs. We could have choose to not send it, and the server would assume 1000.

As for the "reason" key, we can do the same, and assume default being empty string. Or we can just do as the suggested snippets and only send the reason if a reason is added.

I've just noticed an issue on uvicorn on the way we retrieve the reason, as we don't check if reason is None, which is allowed by the spec. I'll be opening a PR there.

Reference for the new ASGI spec: https://asgi.readthedocs.io/en/latest/specs/www.html#close-send-event

Comment thread starlette/websockets.py Outdated
Comment thread starlette/websockets.py

class WebSocketClose:
def __init__(self, code: int = 1000) -> None:
def __init__(self, code: int = 1000, reason: typing.Optional[str] = "") -> None:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __init__(self, code: int = 1000, reason: typing.Optional[str] = "") -> None:
def __init__(self, code: int = 1000, reason: str = None) -> None:

Comment thread starlette/websockets.py Outdated
@Kludex

Kludex commented Nov 22, 2021

Copy link
Copy Markdown
Owner

Re "it's not part of the spec", django/asgiref@6469d0f

@michallowasrzechonek-silvair I didn't get what you said. 馃槜

Cobalt and others added 2 commits November 22, 2021 19:57
Co-authored-by: Marcelo Trylesinski <[email protected]>
Co-authored-by: Marcelo Trylesinski <[email protected]>
@Kludex

Kludex commented Nov 22, 2021

Copy link
Copy Markdown
Owner

Would you mind also adding tests for this? 馃檹

@Chaostheorie

Copy link
Copy Markdown
Author

I will fix it tomorrow afternoon got some studying to do

@Kludex

Kludex commented Jan 8, 2022

Copy link
Copy Markdown
Owner

@Chaostheorie Are you still interested on working on this?

@Chaostheorie

Copy link
Copy Markdown
Author

@Kludex Not really. Sorry to leave this PR hanging, will close it asap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Reason for websocket.close

3 participants