From cbdeda8e45cc98bb1d0c23bcc2123124280b6e06 Mon Sep 17 00:00:00 2001 From: Yang Liu Date: Fri, 19 Sep 2025 16:19:12 +0800 Subject: [PATCH 1/3] Add ping_data parameter to connection class --- src/websockets/asyncio/connection.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/websockets/asyncio/connection.py b/src/websockets/asyncio/connection.py index 592480f9..b4058952 100644 --- a/src/websockets/asyncio/connection.py +++ b/src/websockets/asyncio/connection.py @@ -55,6 +55,7 @@ def __init__( *, ping_interval: float | None = 20, ping_timeout: float | None = 20, + ping_data: Data | None = None, close_timeout: float | None = 10, max_queue: int | None | tuple[int | None, int | None] = 16, write_limit: int | tuple[int, int | None] = 2**15, @@ -62,6 +63,7 @@ def __init__( self.protocol = protocol self.ping_interval = ping_interval self.ping_timeout = ping_timeout + self.ping_data = ping_data self.close_timeout = close_timeout self.max_queue: tuple[int | None, int | None] if isinstance(max_queue, int) or max_queue is None: @@ -825,7 +827,7 @@ async def keepalive(self) -> None: # connection to be closed before raising ConnectionClosed. # However, connection_lost() cancels keepalive_task before # it gets a chance to resume excuting. - pong_waiter = await self.ping() + pong_waiter = await self.ping(self.ping_data) if self.debug: self.logger.debug("% sent keepalive ping") From b6e8c9f2aed690592f7ad8a06538a5a1f6bba979 Mon Sep 17 00:00:00 2001 From: liuyangc3 Date: Fri, 19 Sep 2025 16:33:20 +0800 Subject: [PATCH 2/3] add ping data to each functions --- src/websockets/asyncio/client.py | 14 ++++++++++---- src/websockets/asyncio/connection.py | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/websockets/asyncio/client.py b/src/websockets/asyncio/client.py index bf50bd6f..08c03f07 100644 --- a/src/websockets/asyncio/client.py +++ b/src/websockets/asyncio/client.py @@ -27,7 +27,7 @@ from ..http11 import USER_AGENT, Response from ..protocol import CONNECTING, Event from ..streams import StreamReader -from ..typing import LoggerLike, Origin, Subprotocol +from ..typing import Data, LoggerLike, Origin, Subprotocol from ..uri import Proxy, WebSocketURI, get_proxy, parse_proxy, parse_uri from .compatibility import TimeoutError, asyncio_timeout from .connection import Connection @@ -55,8 +55,8 @@ class ClientConnection(Connection): :exc:`~websockets.exceptions.ConnectionClosedError` when the connection is closed with any other code. - The ``ping_interval``, ``ping_timeout``, ``close_timeout``, ``max_queue``, - and ``write_limit`` arguments have the same meaning as in :func:`connect`. + The ``ping_interval``, ``ping_timeout``, ``ping_data``, ``close_timeout``, + ``max_queue`` and ``write_limit`` arguments have the same meaning as in :func:`connect`. Args: protocol: Sans-I/O connection. @@ -69,6 +69,7 @@ def __init__( *, ping_interval: float | None = 20, ping_timeout: float | None = 20, + ping_data: Data | None = None, close_timeout: float | None = 10, max_queue: int | None | tuple[int | None, int | None] = 16, write_limit: int | tuple[int, int | None] = 2**15, @@ -78,6 +79,7 @@ def __init__( protocol, ping_interval=ping_interval, ping_timeout=ping_timeout, + ping_data=ping_data, close_timeout=close_timeout, max_queue=max_queue, write_limit=write_limit, @@ -233,6 +235,8 @@ class connect: :obj:`None` disables keepalive. ping_timeout: Timeout for keepalive pings in seconds. :obj:`None` disables timeouts. + ping_data: Payload to send in keepalive pings. + :obj:`None` sends an ramdon bytes ping. close_timeout: Timeout for closing the connection in seconds. :obj:`None` disables the timeout. max_size: Maximum size of incoming messages in bytes. @@ -246,7 +250,7 @@ class connect: you may set it to ``None``, although that's a bad idea. write_limit: High-water mark of write buffer in bytes. It is passed to :meth:`~asyncio.WriteTransport.set_write_buffer_limits`. It defaults - to 32 KiB. You may pass a ``(high, low)`` tuple to set the + to 32KiB. You may pass a ``(high, low)`` tuple to set the high-water and low-water marks. logger: Logger for this client. It defaults to ``logging.getLogger("websockets.client")``. @@ -314,6 +318,7 @@ def __init__( open_timeout: float | None = 10, ping_interval: float | None = 20, ping_timeout: float | None = 20, + ping_data: Data | None = None, close_timeout: float | None = 10, # Limits max_size: int | None | tuple[int | None, int | None] = 2**20, @@ -357,6 +362,7 @@ def protocol_factory(uri: WebSocketURI) -> ClientConnection: protocol, ping_interval=ping_interval, ping_timeout=ping_timeout, + ping_data=ping_data, close_timeout=close_timeout, max_queue=max_queue, write_limit=write_limit, diff --git a/src/websockets/asyncio/connection.py b/src/websockets/asyncio/connection.py index b4058952..af2fff88 100644 --- a/src/websockets/asyncio/connection.py +++ b/src/websockets/asyncio/connection.py @@ -826,7 +826,7 @@ async def keepalive(self) -> None: # closing because ping(), via send_context(), waits for the # connection to be closed before raising ConnectionClosed. # However, connection_lost() cancels keepalive_task before - # it gets a chance to resume excuting. + # it gets a chance to resume executing. pong_waiter = await self.ping(self.ping_data) if self.debug: self.logger.debug("% sent keepalive ping") From e79c89c012c3fafeaec92729952c686d09cf89b2 Mon Sep 17 00:00:00 2001 From: liuyangc3 Date: Fri, 19 Sep 2025 16:35:53 +0800 Subject: [PATCH 3/3] add ping data to each functions --- src/websockets/asyncio/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/websockets/asyncio/client.py b/src/websockets/asyncio/client.py index 08c03f07..720fe7da 100644 --- a/src/websockets/asyncio/client.py +++ b/src/websockets/asyncio/client.py @@ -250,7 +250,7 @@ class connect: you may set it to ``None``, although that's a bad idea. write_limit: High-water mark of write buffer in bytes. It is passed to :meth:`~asyncio.WriteTransport.set_write_buffer_limits`. It defaults - to 32KiB. You may pass a ``(high, low)`` tuple to set the + to 32 KiB. You may pass a ``(high, low)`` tuple to set the high-water and low-water marks. logger: Logger for this client. It defaults to ``logging.getLogger("websockets.client")``.