@@ -52,8 +52,8 @@ class ServerConnection(Connection):
52
52
:exc:`~websockets.exceptions.ConnectionClosedError` when the connection is
53
53
closed with any other code.
54
54
55
- The ``close_timeout`` and ``max_queue`` arguments have the same meaning as
56
- in :func:`serve`.
55
+ The ``ping_interval``, ``ping_timeout``, ``close_timeout``, and
56
+ ``max_queue`` arguments have the same meaning as in :func:`serve`.
57
57
58
58
Args:
59
59
socket: Socket connected to a WebSocket client.
@@ -66,6 +66,8 @@ def __init__(
66
66
socket : socket .socket ,
67
67
protocol : ServerProtocol ,
68
68
* ,
69
+ ping_interval : float | None = 20 ,
70
+ ping_timeout : float | None = 20 ,
69
71
close_timeout : float | None = 10 ,
70
72
max_queue : int | None | tuple [int | None , int | None ] = 16 ,
71
73
) -> None :
@@ -74,6 +76,8 @@ def __init__(
74
76
super ().__init__ (
75
77
socket ,
76
78
protocol ,
79
+ ping_interval = ping_interval ,
80
+ ping_timeout = ping_timeout ,
77
81
close_timeout = close_timeout ,
78
82
max_queue = max_queue ,
79
83
)
@@ -354,6 +358,8 @@ def serve(
354
358
compression : str | None = "deflate" ,
355
359
# Timeouts
356
360
open_timeout : float | None = 10 ,
361
+ ping_interval : float | None = 20 ,
362
+ ping_timeout : float | None = 20 ,
357
363
close_timeout : float | None = 10 ,
358
364
# Limits
359
365
max_size : int | None = 2 ** 20 ,
@@ -434,6 +440,10 @@ def handler(websocket):
434
440
:doc:`compression guide <../../topics/compression>` for details.
435
441
open_timeout: Timeout for opening connections in seconds.
436
442
:obj:`None` disables the timeout.
443
+ ping_interval: Interval between keepalive pings in seconds.
444
+ :obj:`None` disables keepalive.
445
+ ping_timeout: Timeout for keepalive pings in seconds.
446
+ :obj:`None` disables timeouts.
437
447
close_timeout: Timeout for closing connections in seconds.
438
448
:obj:`None` disables the timeout.
439
449
max_size: Maximum size of incoming messages in bytes.
@@ -563,6 +573,8 @@ def protocol_select_subprotocol(
563
573
connection = create_connection (
564
574
sock ,
565
575
protocol ,
576
+ ping_interval = ping_interval ,
577
+ ping_timeout = ping_timeout ,
566
578
close_timeout = close_timeout ,
567
579
max_queue = max_queue ,
568
580
)
@@ -590,6 +602,7 @@ def protocol_select_subprotocol(
590
602
591
603
assert connection .protocol .state is OPEN
592
604
try :
605
+ connection .start_keepalive ()
593
606
handler (connection )
594
607
except Exception :
595
608
connection .logger .error ("connection handler failed" , exc_info = True )
0 commit comments