Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions network/v11/ws_reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ async def create_connection(self) -> None:
)

async def reconnect(self) -> None:
# 但愿别 tm 出 bug
# 先关闭旧连接,避免下游服务器保留旧连接拒绝新连接
if hasattr(self, "ws"):
try:
await self.ws.close()
except Exception:
pass # 忽略关闭时的错误
self.ws = None

if not self.connect_task:
self.connect_task = asyncio.create_task(self.connect())
await self.connect_task
Expand Down Expand Up @@ -141,9 +148,22 @@ def __init__(self, config: dict) -> None:
self.role = "Event"

async def push_event(self, event: dict) -> None:
if not hasattr(self, "ws"):
if not self.is_ready():
await self.reconnect()
try:
await self.ws.send(json.dumps(await translator.translate_event(event)))
except websockets.exceptions.ConnectionClosedError:
logger.warning(
f"从反向 WebSocket {self.role} 断开连接: {traceback.format_exc()}"
)
await self.reconnect()
await self.ws.send(json.dumps(await translator.translate_event(event)))
# 重连后重试发送
try:
await self.ws.send(json.dumps(await translator.translate_event(event)))
except Exception:
logger.error(
f"重连后发送事件失败: {traceback.format_exc()}"
)


class UniversalClient(APIClient, EventClient):
Expand Down
Loading