Skip to content
This repository has been archived by the owner on Mar 4, 2023. It is now read-only.

Commit

Permalink
🐛修复ws重复连接情况
Browse files Browse the repository at this point in the history
  • Loading branch information
JustUndertaker committed Feb 7, 2022
1 parent b520f49 commit c96a8ce
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/managers/server_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async def _(bot: Bot, event: PrivateMessageEvent):
@connect_ws.handle()
async def _(bot: Bot, event: PrivateMessageEvent):
'''连接服务器'''
if ws_client.closed:
if ws_client.closed and not ws_client.is_connecting:
msg = "正在连接ws服务器……"
await connect_ws.send(msg)
flag = await ws_client.init()
Expand All @@ -115,14 +115,15 @@ async def _(bot: Bot, event: PrivateMessageEvent):
msg = "ws服务器连接失败!"
await connect_ws.finish(msg)
else:
msg = "ws服务器当前已连接,不要重复连接!"
msg = "ws服务器当前已连接或连接中,请不要重复连接!"
await connect_ws.finish(msg)


@close_ws.handle()
async def _(bot: Bot, event: PrivateMessageEvent):
'''关闭连接'''
await ws_client.close()
if not ws_client.closed:
await ws_client.close()
await close_ws.finish("ws连接已关闭!")

# ----------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions src/managers/server_manager/_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Jx3WebSocket(object):
'''抓马播报'''
_fuyao: bool = False
'''扶摇播报'''
is_connecting: bool = False
'''是否在连接中'''

def __new__(cls, *args, **kwargs):
'''单例'''
Expand Down Expand Up @@ -100,6 +102,7 @@ async def init(self) -> bool:
ws_token = ""
headers = {"token": ws_token}
logger.debug(f"<g>ws_server</g> | 正在链接jx3api的ws服务器:{ws_path}")
self.is_connecting = True
for i in range(1, 101):
try:
logger.debug(
Expand All @@ -110,6 +113,7 @@ async def init(self) -> bool:
ping_interval=20,
ping_timeout=20,
close_timeout=10)
self.is_connecting = False
asyncio.create_task(self._task())
logger.debug(
"<g>ws_server</g> | ws连接成功!"
Expand All @@ -119,6 +123,7 @@ async def init(self) -> bool:
logger.error(
f"<r>链接到ws服务器时发生错误:{str(e)}</r>")
asyncio.sleep(1)
self.is_connecting = False
return False

async def close(self):
Expand Down

0 comments on commit c96a8ce

Please sign in to comment.