Skip to content

Commit b6e8c9f

Browse files
committed
add ping data to each functions
1 parent cbdeda8 commit b6e8c9f

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/websockets/asyncio/client.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from ..http11 import USER_AGENT, Response
2828
from ..protocol import CONNECTING, Event
2929
from ..streams import StreamReader
30-
from ..typing import LoggerLike, Origin, Subprotocol
30+
from ..typing import Data, LoggerLike, Origin, Subprotocol
3131
from ..uri import Proxy, WebSocketURI, get_proxy, parse_proxy, parse_uri
3232
from .compatibility import TimeoutError, asyncio_timeout
3333
from .connection import Connection
@@ -55,8 +55,8 @@ class ClientConnection(Connection):
5555
:exc:`~websockets.exceptions.ConnectionClosedError` when the connection is
5656
closed with any other code.
5757
58-
The ``ping_interval``, ``ping_timeout``, ``close_timeout``, ``max_queue``,
59-
and ``write_limit`` arguments have the same meaning as in :func:`connect`.
58+
The ``ping_interval``, ``ping_timeout``, ``ping_data``, ``close_timeout``,
59+
``max_queue`` and ``write_limit`` arguments have the same meaning as in :func:`connect`.
6060
6161
Args:
6262
protocol: Sans-I/O connection.
@@ -69,6 +69,7 @@ def __init__(
6969
*,
7070
ping_interval: float | None = 20,
7171
ping_timeout: float | None = 20,
72+
ping_data: Data | None = None,
7273
close_timeout: float | None = 10,
7374
max_queue: int | None | tuple[int | None, int | None] = 16,
7475
write_limit: int | tuple[int, int | None] = 2**15,
@@ -78,6 +79,7 @@ def __init__(
7879
protocol,
7980
ping_interval=ping_interval,
8081
ping_timeout=ping_timeout,
82+
ping_data=ping_data,
8183
close_timeout=close_timeout,
8284
max_queue=max_queue,
8385
write_limit=write_limit,
@@ -233,6 +235,8 @@ class connect:
233235
:obj:`None` disables keepalive.
234236
ping_timeout: Timeout for keepalive pings in seconds.
235237
:obj:`None` disables timeouts.
238+
ping_data: Payload to send in keepalive pings.
239+
:obj:`None` sends an ramdon bytes ping.
236240
close_timeout: Timeout for closing the connection in seconds.
237241
:obj:`None` disables the timeout.
238242
max_size: Maximum size of incoming messages in bytes.
@@ -246,7 +250,7 @@ class connect:
246250
you may set it to ``None``, although that's a bad idea.
247251
write_limit: High-water mark of write buffer in bytes. It is passed to
248252
:meth:`~asyncio.WriteTransport.set_write_buffer_limits`. It defaults
249-
to 32 KiB. You may pass a ``(high, low)`` tuple to set the
253+
to 32KiB. You may pass a ``(high, low)`` tuple to set the
250254
high-water and low-water marks.
251255
logger: Logger for this client.
252256
It defaults to ``logging.getLogger("websockets.client")``.
@@ -314,6 +318,7 @@ def __init__(
314318
open_timeout: float | None = 10,
315319
ping_interval: float | None = 20,
316320
ping_timeout: float | None = 20,
321+
ping_data: Data | None = None,
317322
close_timeout: float | None = 10,
318323
# Limits
319324
max_size: int | None | tuple[int | None, int | None] = 2**20,
@@ -357,6 +362,7 @@ def protocol_factory(uri: WebSocketURI) -> ClientConnection:
357362
protocol,
358363
ping_interval=ping_interval,
359364
ping_timeout=ping_timeout,
365+
ping_data=ping_data,
360366
close_timeout=close_timeout,
361367
max_queue=max_queue,
362368
write_limit=write_limit,

src/websockets/asyncio/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ async def keepalive(self) -> None:
826826
# closing because ping(), via send_context(), waits for the
827827
# connection to be closed before raising ConnectionClosed.
828828
# However, connection_lost() cancels keepalive_task before
829-
# it gets a chance to resume excuting.
829+
# it gets a chance to resume executing.
830830
pong_waiter = await self.ping(self.ping_data)
831831
if self.debug:
832832
self.logger.debug("% sent keepalive ping")

0 commit comments

Comments
 (0)