Skip to content

Commit

Permalink
Add close_code and close_reason to new implementations.
Browse files Browse the repository at this point in the history
Also add state to threading implementation.

Fix #1537.
  • Loading branch information
aaugustin committed Nov 11, 2024
1 parent 9a2f39f commit 040ccbb
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/reference/asyncio/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,10 @@ Using a connection
.. autoattribute:: response

.. autoproperty:: subprotocol

The following attributes are available after the closing handshake,
once the WebSocket connection is closed:

.. autoproperty:: close_code

.. autoproperty:: close_reason
7 changes: 7 additions & 0 deletions docs/reference/asyncio/common.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ Both sides (new :mod:`asyncio`)
.. autoattribute:: response

.. autoproperty:: subprotocol

The following attributes are available after the closing handshake,
once the WebSocket connection is closed:

.. autoproperty:: close_code

.. autoproperty:: close_reason
7 changes: 7 additions & 0 deletions docs/reference/asyncio/server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ Using a connection

.. autoproperty:: subprotocol

The following attributes are available after the closing handshake,
once the WebSocket connection is closed:

.. autoproperty:: close_code

.. autoproperty:: close_reason

Broadcast
---------

Expand Down
9 changes: 9 additions & 0 deletions docs/reference/sync/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Using a connection

.. autoproperty:: remote_address

.. autoproperty:: state

The following attributes are available after the opening handshake,
once the WebSocket connection is open:

Expand All @@ -47,3 +49,10 @@ Using a connection
.. autoattribute:: response

.. autoproperty:: subprotocol

The following attributes are available after the closing handshake,
once the WebSocket connection is closed:

.. autoproperty:: close_code

.. autoproperty:: close_reason
9 changes: 9 additions & 0 deletions docs/reference/sync/common.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Both sides (:mod:`threading`)

.. autoproperty:: remote_address

.. autoproperty:: state

The following attributes are available after the opening handshake,
once the WebSocket connection is open:

Expand All @@ -39,3 +41,10 @@ Both sides (:mod:`threading`)
.. autoattribute:: response

.. autoproperty:: subprotocol

The following attributes are available after the closing handshake,
once the WebSocket connection is closed:

.. autoproperty:: close_code

.. autoproperty:: close_reason
9 changes: 9 additions & 0 deletions docs/reference/sync/server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Using a connection

.. autoproperty:: remote_address

.. autoproperty:: state

The following attributes are available after the opening handshake,
once the WebSocket connection is open:

Expand All @@ -61,6 +63,13 @@ Using a connection

.. autoproperty:: subprotocol

The following attributes are available after the closing handshake,
once the WebSocket connection is closed:

.. autoproperty:: close_code

.. autoproperty:: close_reason

HTTP Basic Authentication
-------------------------

Expand Down
24 changes: 24 additions & 0 deletions src/websockets/asyncio/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,30 @@ def subprotocol(self) -> Subprotocol | None:
"""
return self.protocol.subprotocol

@property
def close_code(self) -> int | None:
"""
State of the WebSocket connection, defined in :rfc:`6455`.
This attribute is provided for completeness. Typical applications
shouldn't check its value. Instead, they should inspect attributes
of :exc:`~websockets.exceptions.ConnectionClosed` exceptions.
"""
return self.protocol.close_code

@property
def close_reason(self) -> str | None:
"""
State of the WebSocket connection, defined in :rfc:`6455`.
This attribute is provided for completeness. Typical applications
shouldn't check its value. Instead, they should inspect attributes
of :exc:`~websockets.exceptions.ConnectionClosed` exceptions.
"""
return self.protocol.close_reason

# Public methods

async def __aenter__(self) -> Connection:
Expand Down
4 changes: 2 additions & 2 deletions src/websockets/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def state(self, state: State) -> None:
@property
def close_code(self) -> int | None:
"""
`WebSocket close code`_.
`WebSocket close code`_ received from the remote endpoint.
.. _WebSocket close code:
https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.5
Expand All @@ -191,7 +191,7 @@ def close_code(self) -> int | None:
@property
def close_reason(self) -> str | None:
"""
`WebSocket close reason`_.
`WebSocket close reason`_ received from the remote endpoint..
.. _WebSocket close reason:
https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.6
Expand Down
37 changes: 37 additions & 0 deletions src/websockets/sync/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ def remote_address(self) -> Any:
"""
return self.socket.getpeername()

@property
def state(self) -> State:
"""
State of the WebSocket connection, defined in :rfc:`6455`.
This attribute is provided for completeness. Typical applications
shouldn't check its value. Instead, they should call :meth:`~recv` or
:meth:`send` and handle :exc:`~websockets.exceptions.ConnectionClosed`
exceptions.
"""
return self.protocol.state

@property
def subprotocol(self) -> Subprotocol | None:
"""
Expand All @@ -150,6 +163,30 @@ def subprotocol(self) -> Subprotocol | None:
"""
return self.protocol.subprotocol

@property
def close_code(self) -> int | None:
"""
State of the WebSocket connection, defined in :rfc:`6455`.
This attribute is provided for completeness. Typical applications
shouldn't check its value. Instead, they should inspect attributes
of :exc:`~websockets.exceptions.ConnectionClosed` exceptions.
"""
return self.protocol.close_code

@property
def close_reason(self) -> str | None:
"""
State of the WebSocket connection, defined in :rfc:`6455`.
This attribute is provided for completeness. Typical applications
shouldn't check its value. Instead, they should inspect attributes
of :exc:`~websockets.exceptions.ConnectionClosed` exceptions.
"""
return self.protocol.close_reason

# Public methods

def __enter__(self) -> Connection:
Expand Down

0 comments on commit 040ccbb

Please sign in to comment.