Skip to content

Commit

Permalink
Added reason to WebSocket Disconnect
Browse files Browse the repository at this point in the history
Clients can send this reason field when disconnecting. This is allowed as per RFC 6455 and supported in browsers JS API, but previously not supported in
ASGI.

Fix #459
  • Loading branch information
frankie567 authored May 6, 2024
1 parent db3ff43 commit ae1440a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions asgiref/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class WebSocketResponseBodyEvent(TypedDict):
class WebSocketDisconnectEvent(TypedDict):
type: Literal["websocket.disconnect"]
code: int
reason: Optional[str]


class WebSocketCloseEvent(TypedDict):
Expand Down
5 changes: 5 additions & 0 deletions specs/www.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This spec has had the following versions:
* ``2.2``: Allow ``None`` in the second item of ``server`` scope value.
* ``2.3``: Added the ``reason`` key to the WebSocket close event.
* ``2.4``: Calling ``send()`` on a closed connection should raise an error
* ``2.5``: Added the ``reason`` key to the WebSocket disconnect event.

Spec versions let you understand what the server you are using understands. If
a server tells you it only supports version ``2.0`` of this spec, then
Expand Down Expand Up @@ -457,6 +458,10 @@ Keys:
was received in the frame from the client, the server should set this to ``1005``
(the default value in the WebSocket specification).

* ``reason`` (*Unicode string*) -- A reason given for the disconnect, can
be any string. Optional; if missing or ``None`` default is empty
string.


Disconnected Client - ``send`` exception
''''''''''''''''''''''''''''''''''''''''
Expand Down

0 comments on commit ae1440a

Please sign in to comment.