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()
except Exception:
i += 1
if i == 3:
await log("[ws] Connection lost, reconnect 3 times failed...")
await sleep(5)
continue
await self.get()
@ -73,8 +75,6 @@ class WebSocket:
break
elif i < 3:
await log("[ws] Connection lost, reconnecting...")
elif i == 3:
await log("[ws] Connection lost, reconnect failed...")
async def get(self):
ws_ = self.connection
@ -120,8 +120,11 @@ ws = WebSocket()
@Hook.on_startup()
async def connect_ws():
await ws.connect()
bot.loop.create_task(ws.keep_alive())
try:
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)
@ -130,7 +133,7 @@ async def websocket_push(message: Message):
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):
if message.arguments:
uri = message.arguments