websocket 修复重连三次失败无提醒的问题

This commit is contained in:
xtaodada 2022-09-01 17:12:21 +08:00
parent 800eaa3f8a
commit e10712f053
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659

View File

@ -63,6 +63,8 @@ class WebSocket:
await self.connect() await self.connect()
except Exception: except Exception:
i += 1 i += 1
if i == 3:
await log("[ws] Connection lost, reconnect 3 times failed...")
await sleep(5) await sleep(5)
continue continue
await self.get() await self.get()
@ -73,8 +75,6 @@ class WebSocket:
break break
elif i < 3: elif i < 3:
await log("[ws] Connection lost, reconnecting...") await log("[ws] Connection lost, reconnecting...")
elif i == 3:
await log("[ws] Connection lost, reconnect failed...")
async def get(self): async def get(self):
ws_ = self.connection ws_ = self.connection
@ -120,8 +120,11 @@ ws = WebSocket()
@Hook.on_startup() @Hook.on_startup()
async def connect_ws(): async def connect_ws():
await ws.connect() try:
bot.loop.create_task(ws.keep_alive()) await ws.connect()
bot.loop.create_task(ws.keep_alive())
except Exception as e:
await log(f"[ws] Connection failed: {e}")
@listener(incoming=True, outgoing=True) @listener(incoming=True, outgoing=True)
@ -130,7 +133,7 @@ async def websocket_push(message: Message):
await ws.push(message.__str__()) await ws.push(message.__str__())
@listener(command="websocket", description="Websocket Connect", parameters="<uri>") @listener(command="ws", description="Websocket Connect", parameters="<uri>")
async def websocket_to_connect(message: Message): async def websocket_to_connect(message: Message):
if message.arguments: if message.arguments:
uri = message.arguments uri = message.arguments