fix: ws消息统一具有id字段

This commit is contained in:
DLmaster361
2025-08-27 23:31:19 +08:00
parent bce9777eae
commit e26ca8e81a
5 changed files with 46 additions and 46 deletions

View File

@@ -40,7 +40,9 @@ async def connect_websocket(websocket: WebSocket):
await Broadcast.put(data) await Broadcast.put(data)
except asyncio.TimeoutError: except asyncio.TimeoutError:
await websocket.send_json( await websocket.send_json(
WebSocketMessage(type="Signal", data={"Ping": "无描述"}).model_dump() WebSocketMessage(
id="Main", type="Signal", data={"Ping": "无描述"}
).model_dump()
) )
except WebSocketDisconnect: except WebSocketDisconnect:
break break

View File

@@ -110,7 +110,7 @@ class _TaskManager:
) )
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=str(task_id), id=str(task_id),
type="Info", type="Info",
data={"Error": "脚本类型不支持"}, data={"Error": "脚本类型不支持"},
).model_dump() ).model_dump()
@@ -136,7 +136,7 @@ class _TaskManager:
) )
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=str(task_id), id=str(task_id),
type="Info", type="Info",
data={"Error": "队列类型不支持"}, data={"Error": "队列类型不支持"},
).model_dump() ).model_dump()
@@ -170,7 +170,7 @@ class _TaskManager:
task["status"] = "跳过" task["status"] = "跳过"
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=str(task_id), id=str(task_id),
type="Update", type="Update",
data={"task_list": task_list}, data={"task_list": task_list},
).model_dump() ).model_dump()
@@ -182,7 +182,7 @@ class _TaskManager:
task["status"] = "运行" task["status"] = "运行"
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=str(task_id), id=str(task_id),
type="Update", type="Update",
data={"task_list": task_list}, data={"task_list": task_list},
).model_dump() ).model_dump()
@@ -199,7 +199,7 @@ class _TaskManager:
) )
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=str(task_id), id=str(task_id),
type="Info", type="Info",
data={"Error": "脚本类型不支持"}, data={"Error": "脚本类型不支持"},
).model_dump() ).model_dump()
@@ -260,7 +260,7 @@ class _TaskManager:
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=str(task_id), type="Signal", data={"Accomplish": "无描述"} id=str(task_id), type="Signal", data={"Accomplish": "无描述"}
).model_dump() ).model_dump()
) )

View File

@@ -733,9 +733,7 @@ class TaskCreateOut(OutBase):
class WebSocketMessage(BaseModel): class WebSocketMessage(BaseModel):
taskId: Optional[str] = Field( id: str = Field(..., description="消息ID, 为Main时表示消息来自主进程")
default=None, description="任务ID, 不存在时表示消息来自主程序"
)
type: Literal["Update", "Message", "Info", "Signal"] = Field( type: Literal["Update", "Message", "Info", "Signal"] = Field(
..., ...,
description="消息类型 Update: 更新数据, Message: 请求弹出对话框, Info: 需要在UI显示的消息, Signal: 程序信号", description="消息类型 Update: 更新数据, Message: 请求弹出对话框, Info: 需要在UI显示的消息, Signal: 程序信号",

View File

@@ -120,7 +120,7 @@ class MaaManager:
logger.error(f"未通过配置检查:{self.check_result}") logger.error(f"未通过配置检查:{self.check_result}")
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, type="Info", data={"Error": self.check_result} id=self.ws_id, type="Info", data={"Error": self.check_result}
).model_dump() ).model_dump()
) )
return return
@@ -187,7 +187,7 @@ class MaaManager:
user["status"] = "运行" user["status"] = "运行"
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={"user_list": self.user_list}, data={"user_list": self.user_list},
).model_dump() ).model_dump()
@@ -196,7 +196,7 @@ class MaaManager:
user["status"] = "跳过" user["status"] = "跳过"
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={"user_list": self.user_list}, data={"user_list": self.user_list},
).model_dump() ).model_dump()
@@ -231,7 +231,7 @@ class MaaManager:
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={"log": "正在执行森空岛签到中\n请稍候~"}, data={"log": "正在执行森空岛签到中\n请稍候~"},
).model_dump() ).model_dump()
@@ -250,7 +250,7 @@ class MaaManager:
) )
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Info", type="Info",
data={ data={
( (
@@ -263,7 +263,7 @@ class MaaManager:
logger.info(f"用户: {user['user_id']} - 森空岛签到失败") logger.info(f"用户: {user['user_id']} - 森空岛签到失败")
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Info", type="Info",
data={ data={
"Error": f"用户 {user['name']} 森空岛签到失败", "Error": f"用户 {user['name']} 森空岛签到失败",
@@ -287,7 +287,7 @@ class MaaManager:
) )
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Info", type="Info",
data={ data={
"Warning": f"用户 {user['name']} 未配置森空岛签到Token跳过森空岛签到" "Warning": f"用户 {user['name']} 未配置森空岛签到Token跳过森空岛签到"
@@ -331,7 +331,7 @@ class MaaManager:
) )
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Info", type="Info",
data={"Error": f"未找到 {user['name']} 的详细配置文件"}, data={"Error": f"未找到 {user['name']} 的详细配置文件"},
).model_dump() ).model_dump()
@@ -342,7 +342,7 @@ class MaaManager:
# 更新当前模式到界面 # 更新当前模式到界面
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={ data={
"user_status": { "user_status": {
@@ -426,7 +426,7 @@ class MaaManager:
logger.exception(f"解析快捷方式时出现异常:{e}") logger.exception(f"解析快捷方式时出现异常:{e}")
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Info", type="Info",
data={ data={
"Error": f"解析快捷方式时出现异常:{e}", "Error": f"解析快捷方式时出现异常:{e}",
@@ -439,7 +439,7 @@ class MaaManager:
logger.error(f"模拟器快捷方式不存在:{self.emulator_path}") logger.error(f"模拟器快捷方式不存在:{self.emulator_path}")
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Info", type="Info",
data={ data={
"Error": f"模拟器快捷方式 {self.emulator_path} 不存在", "Error": f"模拟器快捷方式 {self.emulator_path} 不存在",
@@ -491,7 +491,7 @@ class MaaManager:
logger.exception(f"释放ADB时出现异常{e}") logger.exception(f"释放ADB时出现异常{e}")
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Info", type="Info",
data={"Warning": f"释放ADB时出现异常{e}"}, data={"Warning": f"释放ADB时出现异常{e}"},
).model_dump() ).model_dump()
@@ -509,7 +509,7 @@ class MaaManager:
logger.exception(f"启动模拟器时出现异常:{e}") logger.exception(f"启动模拟器时出现异常:{e}")
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Info", type="Info",
data={ data={
"Error": "启动模拟器时出现异常请检查MAA中模拟器路径设置" "Error": "启动模拟器时出现异常请检查MAA中模拟器路径设置"
@@ -557,7 +557,7 @@ class MaaManager:
) )
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={ data={
"log": "检测到MAA进程完成代理任务\n正在等待相关程序结束\n请等待10s" "log": "检测到MAA进程完成代理任务\n正在等待相关程序结束\n请等待10s"
@@ -573,7 +573,7 @@ class MaaManager:
# 此时log变量内存储的就是出现异常的日志信息可以保存或发送用于问题排查 # 此时log变量内存储的就是出现异常的日志信息可以保存或发送用于问题排查
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={ data={
"log": f"{self.maa_result}\n正在中止相关程序\n请等待10s" "log": f"{self.maa_result}\n正在中止相关程序\n请等待10s"
@@ -617,7 +617,7 @@ class MaaManager:
logger.exception(f"释放ADB时出现异常{e}") logger.exception(f"释放ADB时出现异常{e}")
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Info", type="Info",
data={"Error": f"释放ADB时出现异常{e}"}, data={"Error": f"释放ADB时出现异常{e}"},
).model_dump() ).model_dump()
@@ -690,7 +690,7 @@ class MaaManager:
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={ data={
"log": "检测到MAA存在更新\nMAA正在执行更新动作\n请等待10s" "log": "检测到MAA存在更新\nMAA正在执行更新动作\n请等待10s"
@@ -731,7 +731,7 @@ class MaaManager:
user["status"] = "运行" user["status"] = "运行"
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={"user_list": self.user_list}, data={"user_list": self.user_list},
).model_dump() ).model_dump()
@@ -778,7 +778,7 @@ class MaaManager:
self.run_book["SignIn"] = True self.run_book["SignIn"] = True
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={"log": "检测到MAA进程成功登录PRTS"}, data={"log": "检测到MAA进程成功登录PRTS"},
).model_dump() ).model_dump()
@@ -789,7 +789,7 @@ class MaaManager:
) )
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={ data={
"log": f"{self.maa_result}\n正在中止相关程序\n请等待10s" "log": f"{self.maa_result}\n正在中止相关程序\n请等待10s"
@@ -812,7 +812,7 @@ class MaaManager:
uid = str(uuid.uuid4()) uid = str(uuid.uuid4())
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Message", type="Message",
data={ data={
"message_id": uid, "message_id": uid,
@@ -832,7 +832,7 @@ class MaaManager:
uid = str(uuid.uuid4()) uid = str(uuid.uuid4())
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Message", type="Message",
data={ data={
"message_id": uid, "message_id": uid,
@@ -850,7 +850,7 @@ class MaaManager:
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={"user_list": self.user_list}, data={"user_list": self.user_list},
).model_dump() ).model_dump()
@@ -931,7 +931,7 @@ class MaaManager:
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, type="Update", data={"user_list": self.user_list} id=self.ws_id, type="Update", data={"user_list": self.user_list}
).model_dump() ).model_dump()
) )
@@ -1110,7 +1110,7 @@ class MaaManager:
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={ data={
"log": f"即将搜索ADB实际地址\n正在等待模拟器完成启动\n请等待{self.wait_time}s" "log": f"即将搜索ADB实际地址\n正在等待模拟器完成启动\n请等待{self.wait_time}s"
@@ -1201,7 +1201,7 @@ class MaaManager:
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, type="Update", data={"log": log} id=self.ws_id, type="Update", data={"log": log}
).model_dump() ).model_dump()
) )
@@ -1628,7 +1628,7 @@ class MaaManager:
) )
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Info", type="Info",
data={ data={
"warning": f"未选择用户 {self.cur_user_data.get('Info', 'Name')} 的自定义基建配置文件" "warning": f"未选择用户 {self.cur_user_data.get('Info', 'Name')} 的自定义基建配置文件"

View File

@@ -162,7 +162,7 @@ class GeneralManager:
logger.error(f"未通过配置检查:{self.check_result}") logger.error(f"未通过配置检查:{self.check_result}")
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, type="Info", data={"Error": self.check_result} id=self.ws_id, type="Info", data={"Error": self.check_result}
).model_dump() ).model_dump()
) )
return return
@@ -229,7 +229,7 @@ class GeneralManager:
user["status"] = "运行" user["status"] = "运行"
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={"user_list": self.user_list}, data={"user_list": self.user_list},
).model_dump() ).model_dump()
@@ -238,7 +238,7 @@ class GeneralManager:
user["status"] = "跳过" user["status"] = "跳过"
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={"user_list": self.user_list}, data={"user_list": self.user_list},
).model_dump() ).model_dump()
@@ -258,7 +258,7 @@ class GeneralManager:
logger.error(f"用户: {user['user_id']} - 未找到配置文件") logger.error(f"用户: {user['user_id']} - 未找到配置文件")
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Info", type="Info",
data={"Error": f"未找到 {user['user_id']} 的配置文件"}, data={"Error": f"未找到 {user['user_id']} 的配置文件"},
).model_dump() ).model_dump()
@@ -311,7 +311,7 @@ class GeneralManager:
logger.exception(f"启动游戏/模拟器时出现异常:{e}") logger.exception(f"启动游戏/模拟器时出现异常:{e}")
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Info", type="Info",
data={"Error": f"启动游戏/模拟器时出现异常:{e}"}, data={"Error": f"启动游戏/模拟器时出现异常:{e}"},
).model_dump() ).model_dump()
@@ -332,7 +332,7 @@ class GeneralManager:
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={ data={
"log": f"正在等待游戏/模拟器完成启动\n请等待{self.script_config.get('Game', 'WaitTime')}s" "log": f"正在等待游戏/模拟器完成启动\n请等待{self.script_config.get('Game', 'WaitTime')}s"
@@ -375,7 +375,7 @@ class GeneralManager:
) )
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={ data={
"log": "检测到通用脚本进程完成代理任务\n正在等待相关程序结束\n请等待10s" "log": "检测到通用脚本进程完成代理任务\n正在等待相关程序结束\n请等待10s"
@@ -433,7 +433,7 @@ class GeneralManager:
# 此时log变量内存储的就是出现异常的日志信息可以保存或发送用于问题排查 # 此时log变量内存储的就是出现异常的日志信息可以保存或发送用于问题排查
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, id=self.ws_id,
type="Update", type="Update",
data={ data={
"log": f"{self.general_result}\n正在中止相关程序\n请等待10s" "log": f"{self.general_result}\n正在中止相关程序\n请等待10s"
@@ -777,7 +777,7 @@ class GeneralManager:
await Config.send_json( await Config.send_json(
WebSocketMessage( WebSocketMessage(
taskId=self.ws_id, type="Update", data={"log": log} id=self.ws_id, type="Update", data={"log": log}
).model_dump() ).model_dump()
) )