From 79ed1d4a4b3907cf4c013b935d3643d0c1de65ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Fri, 22 Nov 2024 11:46:51 +0800 Subject: [PATCH] Added heartbeat --- roc/socket.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/roc/socket.py b/roc/socket.py index 2522e65..e54a3b8 100644 --- a/roc/socket.py +++ b/roc/socket.py @@ -12,6 +12,7 @@ from roc.request import Request from roc.request import Response from roc.request import make_response +from roc.packet import PING class SocketException(Exception): @@ -45,6 +46,8 @@ async def loop(self): body = await self.recv(length) packet = self.packer.unpack(prefix + body) + if packet.is_heartbeat(): + continue chan = self.channelManager.get(packet.id) if chan is not None: @@ -55,6 +58,17 @@ async def loop(self): self.reader = None break + async def heartbeat(self): + while True: + try: + await self.send(Packet(0, PING)) + + await asyncio.sleep(10) + except SocketException: + self.writer = None + self.reader = None + break + async def recv(self, length: int) -> bytes: result: bytes = b'' while True: @@ -98,3 +112,4 @@ async def start(self): self.writer = writer asyncio.create_task(self.loop()) + asyncio.create_task(self.heartbeat())