Skip to content

Commit

Permalink
Added reason to WebSocket Disconnect (#462)
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 Jun 12, 2024
1 parent db3ff43 commit f86642b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
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
7 changes: 6 additions & 1 deletion specs/www.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
HTTP & WebSocket ASGI Message Format
====================================

**Version**: 2.4 (2024-01-16)
**Version**: 2.5 (2024-06-05)

The HTTP+WebSocket ASGI sub-specification outlines how to transport HTTP/1.1,
HTTP/2 and WebSocket connections within ASGI.
Expand All @@ -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 f86642b

Please sign in to comment.