⚗️ add option for ws reconnect

This commit is contained in:
yanyongyu 2021-11-30 12:24:09 +08:00
parent a6112fdf64
commit b92c1a0b33
3 changed files with 15 additions and 1 deletions

View File

@ -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 重连间隔"""

View File

@ -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:

View File

@ -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: