fix(bridge): WS桥接统一回显_msgId/_callId,响应严格按ID匹配避免广播误匹配

This commit is contained in:
liufumingren 2026-08-24 02:20:48 +08:00
parent 5bb8c30e12
commit ffd2d238c2

View File

@ -250,9 +250,11 @@ class WSServer {
ws.send(JSON.stringify({ success: false, message: '消息解析失败' })); ws.send(JSON.stringify({ success: false, message: '消息解析失败' }));
return; return;
} }
const { method, data, _callId } = parsed; const { method, data, _callId, _msgId } = parsed;
// 统一回显请求 ID:优先 _msgId(Electron 桥接),兼容 _callId(页面直连)
const replyId = _msgId || _callId;
if (!method) { if (!method) {
ws.send(JSON.stringify({ success: false, message: 'method不能为空', _callId })); ws.send(JSON.stringify({ success: false, message: 'method不能为空', _msgId: replyId, _callId: replyId }));
return; return;
} }
// 支持多级目录结构,如 index.index.index // 支持多级目录结构,如 index.index.index
@ -265,17 +267,18 @@ class WSServer {
if (typeof serviceFn === 'function') { if (typeof serviceFn === 'function') {
try { try {
const result = await serviceFn(data); const result = await serviceFn(data);
ws.send(JSON.stringify({ success: true, method, result, _callId })); ws.send(JSON.stringify({ success: true, method, result, _msgId: replyId, _callId: replyId }));
} catch (err) { } catch (err) {
//console.log(err); //console.log(err);
ws.send(JSON.stringify({ success: false, method, message: '服务方法执行失败: ' + err.message, _callId })); ws.send(JSON.stringify({ success: false, method, message: '服务方法执行失败: ' + err.message, _msgId: replyId, _callId: replyId }));
} }
} else { } else {
ws.send(JSON.stringify({ ws.send(JSON.stringify({
success: false, success: false,
method, method,
message: '未找到对应服务方法', message: '未找到对应服务方法',
_callId, _msgId: replyId,
_callId: replyId,
debug: { debug: {
method, method,
methodPath, methodPath,