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())