Skip to content
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

Add reason support for websocket.close #1339

Closed
wants to merge 3 commits into from
Closed

Add reason support for websocket.close #1339

wants to merge 3 commits into from

Conversation

Chaostheorie
Copy link

Recreate old changes

Carbon-Copy of #992
Aims to resolve #991

@michallowasrzechonek-silvair

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

Copy link
Member

@Kludex Kludex left a comment

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

starlette/websockets.py Outdated Show resolved Hide resolved


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

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:

starlette/websockets.py Outdated Show resolved Hide resolved
@Kludex
Copy link
Member

Kludex commented Nov 22, 2021

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
Copy link
Member

Kludex commented Nov 22, 2021

Would you mind also adding tests for this? 🙏

@Chaostheorie
Copy link
Author

I will fix it tomorrow afternoon got some studying to do

@Kludex
Copy link
Member

Kludex commented Jan 8, 2022

@Chaostheorie Are you still interested on working on this?

@Chaostheorie
Copy link
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