Skip to content

Commit

Permalink
Added heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo committed Nov 22, 2024
1 parent 7075ad1 commit 79ed1d4
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions roc/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -98,3 +112,4 @@ async def start(self):
self.writer = writer

asyncio.create_task(self.loop())
asyncio.create_task(self.heartbeat())

0 comments on commit 79ed1d4

Please sign in to comment.