support ping_interval and ping_timeout config

This commit is contained in:
StarHeartHunt
2026-04-12 16:29:23 +08:00
parent 738a193c49
commit 74b5ff3567
3 changed files with 23 additions and 2 deletions

View File

@@ -324,6 +324,10 @@ class Mixin(HTTPClientMixin, WebSocketClientMixin):
)
)
autoping = True
if setup.ping_interval is not UNSET:
autoping = setup.ping_interval is not None
async with aiohttp.ClientSession(version=version, trust_env=True) as session:
async with session.ws_connect(
setup.url,
@@ -331,6 +335,7 @@ class Mixin(HTTPClientMixin, WebSocketClientMixin):
timeout=timeout,
headers=setup.headers,
proxy=setup.proxy,
autoping=autoping,
) as ws:
yield WebSocket(request=setup, session=session, websocket=ws)

View File

@@ -83,7 +83,11 @@ class Mixin(WebSocketClientMixin):
setup.timeout.connect or setup.timeout.read or setup.timeout.total
)
timeout_kwargs = exclude_unset(
{"open_timeout": open_timeout, "close_timeout": setup.timeout.close}
{
"open_timeout": open_timeout,
"close_timeout": setup.timeout.close,
"ping_timeout": setup.timeout.ping,
}
)
elif setup.timeout is not UNSET:
timeout_kwargs = {
@@ -102,11 +106,18 @@ class Mixin(WebSocketClientMixin):
}
)
kwargs = exclude_unset(
{
**timeout_kwargs,
"ping_interval": setup.ping_interval,
}
)
connection = connect(
str(setup.url),
additional_headers={**setup.headers, **setup.cookies.as_header(setup)},
proxy=setup.proxy if setup.proxy is not None else True,
**timeout_kwargs, # type: ignore
**kwargs, # type: ignore
)
async with connection as ws:
yield WebSocket(request=setup, websocket=ws)

View File

@@ -20,6 +20,7 @@ class Timeout:
connect: float | None | UnsetType = UNSET
read: float | None | UnsetType = UNSET
close: float | None | UnsetType = UNSET
ping: float | None | UnsetType = UNSET
DEFAULT_TIMEOUT = Timeout(total=None, connect=5.0, read=30.0, close=10.0)
@@ -52,6 +53,7 @@ FileTypes: TypeAlias = (
)
FilesTypes: TypeAlias = dict[str, FileTypes] | list[tuple[str, FileTypes]] | None
TimeoutTypes: TypeAlias = float | Timeout | None
PingIntervalTypes: TypeAlias = float | None
class HTTPVersion(Enum):
@@ -76,6 +78,7 @@ class Request:
version: str | HTTPVersion = HTTPVersion.H11,
timeout: TimeoutTypes | UnsetType = UNSET,
proxy: str | None = None,
ping_interval: PingIntervalTypes | UnsetType = UNSET,
):
# method
self.method: str = (
@@ -89,6 +92,8 @@ class Request:
self.timeout: TimeoutTypes | UnsetType = timeout
# proxy
self.proxy: str | None = proxy
# ping interval
self.ping_interval: PingIntervalTypes | UnsetType = ping_interval
# url
if isinstance(url, tuple):