fix: add WebSocket keepalive pings to prevent idle disconnects (#646) (#925)

This commit is contained in:
Konukhov Yaroslav
2026-06-17 00:13:55 -07:00
committed by GitHub
parent 13d2b1db6d
commit 96e5b4b639
4 changed files with 34 additions and 1 deletions

View File

@@ -154,6 +154,13 @@ class RawWebSocket:
self._build_frame(self.OP_BINARY, part, mask=True))
await self.writer.drain()
async def send_ping(self, payload: bytes = b''):
if self._closed:
raise ConnectionError("WebSocket closed")
frame = self._build_frame(self.OP_PING, payload, mask=True)
self.writer.write(frame)
await self.writer.drain()
async def recv(self) -> Optional[bytes]:
while not self._closed:
opcode, payload = await self._read_frame()