mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-28 05:06:56 +08:00
⚗️ add option for ws reconnect
This commit is contained in:
parent
a6112fdf64
commit
b92c1a0b33
@ -430,5 +430,7 @@ class WebSocketSetup:
|
|||||||
"""URL"""
|
"""URL"""
|
||||||
headers: Dict[str, str] = field(default_factory=dict)
|
headers: Dict[str, str] = field(default_factory=dict)
|
||||||
"""HTTP headers"""
|
"""HTTP headers"""
|
||||||
|
reconnect: bool = True
|
||||||
|
"""WebSocket 是否重连"""
|
||||||
reconnect_interval: float = 3.0
|
reconnect_interval: float = 3.0
|
||||||
"""WebSocket 重连间隔"""
|
"""WebSocket 重连间隔"""
|
||||||
|
@ -407,6 +407,12 @@ class Driver(ForwardDriver):
|
|||||||
if bot:
|
if bot:
|
||||||
self._bot_disconnect(bot)
|
self._bot_disconnect(bot)
|
||||||
bot = None
|
bot = None
|
||||||
|
|
||||||
|
if not setup_.reconnect:
|
||||||
|
logger.info(
|
||||||
|
f"WebSocket reconnect disabled for bot {setup_.self_id}"
|
||||||
|
)
|
||||||
|
break
|
||||||
await asyncio.sleep(setup_.reconnect_interval)
|
await asyncio.sleep(setup_.reconnect_interval)
|
||||||
|
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
|
@ -555,7 +555,7 @@ class FullDriver(ForwardDriver, Driver):
|
|||||||
asyncio.create_task(bot.handle_message(msg))
|
asyncio.create_task(bot.handle_message(msg))
|
||||||
except ConnectionClosed:
|
except ConnectionClosed:
|
||||||
logger.opt(colors=True).error(
|
logger.opt(colors=True).error(
|
||||||
"<r><bg #f8bbd0>WebSocket connection closed by peer. "
|
"<r><bg #f8bbd0>WebSocket connection closed. "
|
||||||
"Try to reconnect...</bg #f8bbd0></r>"
|
"Try to reconnect...</bg #f8bbd0></r>"
|
||||||
)
|
)
|
||||||
break
|
break
|
||||||
@ -568,6 +568,12 @@ class FullDriver(ForwardDriver, Driver):
|
|||||||
if bot:
|
if bot:
|
||||||
self._bot_disconnect(bot)
|
self._bot_disconnect(bot)
|
||||||
bot = None
|
bot = None
|
||||||
|
|
||||||
|
if not setup_.reconnect:
|
||||||
|
logger.info(
|
||||||
|
f"WebSocket reconnect disabled for bot {setup_.self_id}"
|
||||||
|
)
|
||||||
|
break
|
||||||
await asyncio.sleep(setup_.reconnect_interval)
|
await asyncio.sleep(setup_.reconnect_interval)
|
||||||
|
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
|
Loading…
Reference in New Issue
Block a user