🎨 优化重连机制

This commit is contained in:
KimigaiiWuyi 2025-04-10 04:32:54 +08:00
parent 788762cab6
commit fdbc3aa3be
3 changed files with 24 additions and 28 deletions

View File

@ -22,22 +22,11 @@ require('nonebot_plugin_apscheduler')
from nonebot_plugin_apscheduler import scheduler # noqa:E402
from .client import GsClient, driver # noqa:E402
from .auto_install import start, install # noqa:E402
from .models import Message, MessageReceive # noqa:E402
get_message = on_message(priority=0)
get_notice = on_notice(priority=0)
get_tn = on('inline')
install_core = on_fullmatch(
'gs一键安装',
permission=SUPERUSER,
block=True,
)
start_core = on_fullmatch(
'启动core',
permission=SUPERUSER,
block=True,
)
connect_core = on_fullmatch(
('连接core', '链接core'),
permission=SUPERUSER,
@ -54,6 +43,7 @@ __plugin_meta__ = PluginMetadata(
)
gsclient: Optional[GsClient] = None
is_connecting = False
command_start = deepcopy(driver.config.command_start)
command_start.discard('')
msg_id_cache = OrderedDict()
@ -325,8 +315,14 @@ async def get_notice_message(bot: Bot, ev: Event):
@get_message.handle()
async def get_all_message(bot: Bot, ev: Event):
if gsclient is None:
global is_connecting
if is_connecting:
return
if gsclient is None and is_connecting is False:
is_connecting = True
return await connect()
try:
await gsclient.ws.ping()
except ConnectionClosed:
@ -730,23 +726,12 @@ async def get_all_message(bot: Bot, ev: Event):
await gsclient._input(msg)
@install_core.handle()
async def send_install_msg(matcher: Matcher):
await matcher.send('即将开始安装...会持续一段时间, 且期间无法使用Bot!')
await matcher.send(await install())
@connect_core.handle()
async def send_connect_msg(matcher: Matcher):
await connect()
await matcher.send('链接成功!')
@start_core.handle()
async def send_start_msg(matcher: Matcher):
await matcher.send(await start())
@driver.on_bot_connect
async def start_client():
if gsclient is None:
@ -756,9 +741,12 @@ async def start_client():
async def connect():
global gsclient
try:
await asyncio.sleep(2)
gsclient = await GsClient().async_connect()
await gsclient.start()
except ConnectionRefusedError:
global is_connecting
is_connecting = False
logger.error('Core服务器连接失败...请稍后使用[启动core]命令启动...')

View File

@ -5,6 +5,7 @@ import uuid
import base64
import asyncio
from pathlib import Path
from asyncio import CancelledError
from collections import OrderedDict
from typing import Dict, List, Union, Optional
@ -312,6 +313,10 @@ class GsClient:
)
except Exception as e:
logger.exception(e)
except CancelledError:
logger.warning(f'与[gsuid-core]断开连接! Bot_ID: {BOT_ID}')
except KeyboardInterrupt:
logger.warning(f'与[gsuid-core]断开连接! Bot_ID: {BOT_ID}')
except RuntimeError as e:
logger.error(e)
except ConnectionClosedError:
@ -331,10 +336,13 @@ class GsClient:
await self.msg_list.put(msg)
async def send_msg(self):
while True:
msg: MessageReceive = await self.msg_list.get()
msg_send = msgjson.encode(msg)
await self.ws.send(msg_send)
try:
while True:
msg: MessageReceive = await self.msg_list.get()
msg_send = msgjson.encode(msg)
await self.ws.send(msg_send)
except CancelledError:
logger.warning(f'与[gsuid-core]断开连接! Bot_ID: {BOT_ID}')
async def start(self):
recv_task = asyncio.create_task(self.recv_msg())

View File

@ -23,7 +23,7 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry]
name = "nonebot-plugin-genshinuid"
version = "4.7.3"
version = "4.8.0"
description = "支持OneBot(QQ)、OneBotV12、QQ频道、微信、KOOK开黑啦、Telegram电报、FeiShu飞书、DoDo、Villa米游社大别野、Discord的全功能NoneBot2原神插件"
authors = ["KimigaiiWuyi <444835641@qq.com>"]
license = "GPL-3.0-or-later"