Skip to content

Commit 8d9f9a1

Browse files
committed
Expose connection state in the new asyncio implementation.
1 parent c3b162d commit 8d9f9a1

File tree

6 files changed

+24
-2
lines changed

6 files changed

+24
-2
lines changed

docs/reference/new-asyncio/client.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Using a connection
4343

4444
.. autoproperty:: remote_address
4545

46+
.. autoproperty:: state
47+
4648
The following attributes are available after the opening handshake,
4749
once the WebSocket connection is open:
4850

docs/reference/new-asyncio/common.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Both sides (new :mod:`asyncio`)
3333

3434
.. autoproperty:: remote_address
3535

36+
.. autoproperty:: state
37+
3638
The following attributes are available after the opening handshake,
3739
once the WebSocket connection is open:
3840

docs/reference/new-asyncio/server.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ Using a connection
6262

6363
.. autoproperty:: remote_address
6464

65+
.. autoproperty:: state
66+
6567
The following attributes are available after the opening handshake,
6668
once the WebSocket connection is open:
6769

src/websockets/asyncio/connection.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ def remote_address(self) -> Any:
137137
"""
138138
return self.transport.get_extra_info("peername")
139139

140+
@property
141+
def state(self) -> State:
142+
"""
143+
State of the WebSocket connection, defined in :rfc:`6455`.
144+
145+
This attribute is provided for completeness. Typical applications
146+
shouldn't check its value. Instead, they should call :meth:`~recv` or
147+
:meth:`send` and handle :exc:`~exceptions.ConnectionClosed` exceptions.
148+
149+
"""
150+
return self.protocol.state
151+
140152
@property
141153
def subprotocol(self) -> Subprotocol | None:
142154
"""

src/websockets/protocol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def __init__(
156156
@property
157157
def state(self) -> State:
158158
"""
159-
WebSocket connection state.
159+
State of the WebSocket connection.
160160
161161
Defined in 4.1, 4.2, 7.1.3, and 7.1.4 of :rfc:`6455`.
162162

tests/asyncio/test_connection.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from websockets.asyncio.connection import *
1212
from websockets.exceptions import ConnectionClosedError, ConnectionClosedOK
1313
from websockets.frames import CloseCode, Frame, Opcode
14-
from websockets.protocol import CLIENT, SERVER, Protocol
14+
from websockets.protocol import CLIENT, SERVER, Protocol, State
1515

1616
from ..protocol import RecordingProtocol
1717
from ..utils import MS
@@ -930,6 +930,10 @@ async def test_remote_address(self, get_extra_info):
930930
self.assertEqual(self.connection.remote_address, ("peer", 1234))
931931
get_extra_info.assert_called_with("peername")
932932

933+
async def test_state(self):
934+
"""Connection has a state attribute."""
935+
self.assertEqual(self.connection.state, State.OPEN)
936+
933937
async def test_request(self):
934938
"""Connection has a request attribute."""
935939
self.assertIsNone(self.connection.request)

0 commit comments

Comments
 (0)