Skip to content

Commit d0c7084

Browse files
committed
feat: 支持定时心跳
Signed-off-by: Ke Jie <[email protected]>
1 parent 4fb7df6 commit d0c7084

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

dingtalk_stream/stream.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ async def start(self):
7575
uri = '%s?ticket=%s' % (connection['endpoint'], urllib.parse.quote_plus(connection['ticket']))
7676
async with websockets.connect(uri) as websocket:
7777
self.websocket = websocket
78+
keepalive_task = asyncio.create_task(self.keepalive(websocket))
7879
async for raw_message in websocket:
7980
json_message = json.loads(raw_message)
8081
asyncio.create_task(self.background_task(json_message))
@@ -92,6 +93,14 @@ async def start(self):
9293
finally:
9394
pass
9495

96+
async def keepalive(self, ws, ping_interval=1):
97+
while True:
98+
await asyncio.sleep(ping_interval)
99+
try:
100+
await ws.ping()
101+
except websockets.exceptions.ConnectionClosed:
102+
break
103+
95104
async def background_task(self, json_message):
96105
try:
97106
route_result = await self.route_message(json_message)

0 commit comments

Comments
 (0)