diff --git a/src/websockets/asyncio/client.py b/src/websockets/asyncio/client.py index bf50bd6f..720fe7da 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. @@ -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 592480f9..af2fff88 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: @@ -824,8 +826,8 @@ 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. - pong_waiter = await self.ping() + # it gets a chance to resume executing. + pong_waiter = await self.ping(self.ping_data) if self.debug: self.logger.debug("% sent keepalive ping")