[patch] 优化心跳检测逻辑 优化与后端交互连接处理逻辑 新增自动登录与手动下发冲突问题解决方法块
This commit is contained in:
@@ -155,8 +155,13 @@ class WebSocketManager:
|
||||
if self.callbacks['disconnect']:
|
||||
self.callbacks['disconnect'](disconnect_msg)
|
||||
|
||||
def _on_log(message: str, level: str = "INFO"):
|
||||
"""Backend client log callback"""
|
||||
self._log(message, level)
|
||||
|
||||
backend.set_callbacks(success=_on_backend_success, login=_on_backend_login,
|
||||
token_error=_on_token_error, disconnect=_on_disconnect)
|
||||
token_error=_on_token_error, disconnect=_on_disconnect,
|
||||
log=_on_log)
|
||||
|
||||
if not backend.is_connected:
|
||||
backend.connect()
|
||||
@@ -196,8 +201,13 @@ class WebSocketManager:
|
||||
if self.callbacks['disconnect']:
|
||||
self.callbacks['disconnect'](disconnect_msg)
|
||||
|
||||
def _on_log(message: str, level: str = "INFO"):
|
||||
"""Backend client log callback"""
|
||||
self._log(message, level)
|
||||
|
||||
backend.set_callbacks(login=_on_backend_login, success=_on_backend_success,
|
||||
token_error=_on_token_error, disconnect=_on_disconnect)
|
||||
token_error=_on_token_error, disconnect=_on_disconnect,
|
||||
log=_on_log)
|
||||
backend.connect()
|
||||
|
||||
set_backend_client(backend)
|
||||
@@ -214,13 +224,92 @@ class WebSocketManager:
|
||||
def _handle_platform_login(self, platform_name: str, store_id: str, cookies: str):
|
||||
"""处理平台登录请求"""
|
||||
try:
|
||||
# 🔥 检查并清理当前店铺的旧连接
|
||||
# 🔥 检查并断开当前店铺的旧连接(策略B:先断开旧连接,再建立新连接)
|
||||
store_key_pattern = f":{store_id}" # 匹配 "平台名:store_id" 格式
|
||||
keys_to_remove = [key for key in self.platform_listeners.keys() if key.endswith(store_key_pattern)]
|
||||
|
||||
if keys_to_remove:
|
||||
self._log(f"🔄 检测到店铺 {store_id} 重连,清理 {len(keys_to_remove)} 个旧连接", "INFO")
|
||||
self._log(f"🔄 检测到店铺 {store_id} 重复登录,断开 {len(keys_to_remove)} 个旧连接", "INFO")
|
||||
|
||||
for key in keys_to_remove:
|
||||
listener_info = self.platform_listeners.get(key)
|
||||
if listener_info:
|
||||
platform_type = listener_info.get('platform', '')
|
||||
|
||||
# 从各平台的 WebsocketManager 中获取连接并关闭WebSocket
|
||||
try:
|
||||
if platform_type == "京东":
|
||||
from Utils.JD.JdUtils import WebsocketManager as JDWSManager
|
||||
jd_mgr = JDWSManager()
|
||||
conn_info = jd_mgr.get_connection(key)
|
||||
if conn_info and conn_info.get('platform'):
|
||||
ws = conn_info['platform'].get('ws')
|
||||
if ws and hasattr(ws, 'close'):
|
||||
try:
|
||||
import asyncio
|
||||
loop = conn_info['platform'].get('loop')
|
||||
if loop and not loop.is_closed():
|
||||
asyncio.run_coroutine_threadsafe(ws.close(), loop)
|
||||
self._log(f"✅ 已关闭京东WebSocket连接: {key}", "DEBUG")
|
||||
except Exception:
|
||||
pass
|
||||
jd_mgr.remove_connection(key)
|
||||
self._log(f"✅ 已从京东管理器移除连接: {key}", "DEBUG")
|
||||
|
||||
elif platform_type == "抖音":
|
||||
from Utils.Dy.DyUtils import DouYinWebsocketManager as DYWSManager
|
||||
dy_mgr = DYWSManager()
|
||||
conn_info = dy_mgr.get_connection(key)
|
||||
if conn_info and conn_info.get('platform'):
|
||||
ws = conn_info['platform'].get('ws')
|
||||
if ws and hasattr(ws, 'close'):
|
||||
try:
|
||||
import asyncio
|
||||
loop = conn_info['platform'].get('loop')
|
||||
if loop and not loop.is_closed():
|
||||
asyncio.run_coroutine_threadsafe(ws.close(), loop)
|
||||
self._log(f"✅ 已关闭抖音WebSocket连接: {key}", "DEBUG")
|
||||
except Exception:
|
||||
pass
|
||||
dy_mgr.remove_connection(key)
|
||||
self._log(f"✅ 已从抖音管理器移除连接: {key}", "DEBUG")
|
||||
|
||||
elif platform_type == "千牛":
|
||||
from Utils.QianNiu.QianNiuUtils import QianNiuWebsocketManager as QNWSManager
|
||||
qn_mgr = QNWSManager()
|
||||
qn_mgr.remove_connection(key)
|
||||
self._log(f"✅ 已从千牛管理器移除连接: {key}", "DEBUG")
|
||||
|
||||
elif platform_type == "拼多多":
|
||||
from Utils.Pdd.PddUtils import WebsocketManager as PDDWSManager
|
||||
pdd_mgr = PDDWSManager()
|
||||
conn_info = pdd_mgr.get_connection(key)
|
||||
if conn_info and conn_info.get('platform'):
|
||||
# 关闭WebSocket连接
|
||||
ws = conn_info['platform'].get('ws')
|
||||
if ws and hasattr(ws, 'close'):
|
||||
try:
|
||||
import asyncio
|
||||
loop = conn_info['platform'].get('loop')
|
||||
if loop and not loop.is_closed():
|
||||
asyncio.run_coroutine_threadsafe(ws.close(), loop)
|
||||
self._log(f"✅ 已关闭拼多多WebSocket连接: {key}", "DEBUG")
|
||||
except Exception as ws_e:
|
||||
self._log(f"⚠️ 关闭WebSocket时出错: {ws_e}", "DEBUG")
|
||||
pdd_mgr.remove_connection(key)
|
||||
self._log(f"✅ 已从拼多多管理器移除连接: {key}", "DEBUG")
|
||||
|
||||
except Exception as e:
|
||||
self._log(f"⚠️ 移除{platform_type}连接时出错: {e}", "WARNING")
|
||||
|
||||
# 从监听器字典中移除
|
||||
self.platform_listeners.pop(key, None)
|
||||
|
||||
# 给WebSocket一点时间完全关闭
|
||||
import time
|
||||
time.sleep(0.5)
|
||||
|
||||
self._log(f"✅ 旧连接已全部断开,准备建立新连接", "INFO")
|
||||
|
||||
# 平台名称映射
|
||||
platform_map = {
|
||||
|
||||
Reference in New Issue
Block a user