fix: 修复测试消息重复显示和详细日志输出
- 删除重复的测试消息通信模块 - 添加 openclaw-log 事件将详细日志发送到界面 - 界面监听并显示 OpenClaw 详细日志 - 日志包含:连接状态、发送请求、接收消息、响应状态、错误诊断 现在连接 OpenClaw 时会看到详细日志: [OpenClaw] ========== 开始连接 ========== [OpenClaw] ✅ WebSocket 连接已建立 | 就绪状态:1 [OpenClaw] 📤 发送 connect 请求... [OpenClaw] 📥 收到消息 (长度:123 字节) [OpenClaw] 响应:✅ 成功 / ❌ 失败
This commit is contained in:
@@ -343,15 +343,22 @@ class OpenClawConnection {
|
||||
this.socket.on('open', () => {
|
||||
this.isConnected = true;
|
||||
this.reconnectAttempts = 0;
|
||||
console.log('[OpenClaw] ✅ WebSocket 连接已建立'); console.log('[OpenClaw] 就绪状态:', this.socket.readyState, '(1=OPEN)');
|
||||
const logMsg = '[OpenClaw] ✅ WebSocket 连接已建立 | 就绪状态:' + this.socket.readyState + ' (1=OPEN)';
|
||||
console.log(logMsg);
|
||||
|
||||
// 检查窗口是否存在
|
||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||
this.eventHandler('connected');
|
||||
// 发送详细日志到界面
|
||||
mainWindow.webContents.send('openclaw-log', { type: 'info', message: logMsg });
|
||||
}
|
||||
|
||||
// 主动发送 connect 请求
|
||||
console.log('[OpenClaw] 📤 发送 connect 请求...');
|
||||
const sendLog = '[OpenClaw] 📤 发送 connect 请求...';
|
||||
console.log(sendLog);
|
||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||
mainWindow.webContents.send('openclaw-log', { type: 'info', message: sendLog });
|
||||
}
|
||||
this.sendConnect();
|
||||
});
|
||||
|
||||
@@ -361,51 +368,57 @@ class OpenClawConnection {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const message = JSON.parse(data.toString());
|
||||
console.log('[OpenClaw] Received:', JSON.stringify(message, null, 2));
|
||||
const messageStr = data.toString();
|
||||
const logMsg = '[OpenClaw] 📥 收到消息 (长度:' + messageStr.length + ' 字节) | 类型:' + messageStr.substring(0, 50);
|
||||
console.log(logMsg);
|
||||
mainWindow.webContents.send('openclaw-log', { type: 'info', message: logMsg });
|
||||
|
||||
const message = JSON.parse(messageStr);
|
||||
|
||||
// 处理响应
|
||||
if (message.type === 'res') {
|
||||
const resLog = '[OpenClaw] 响应:' + (message.ok ? '✅ 成功' : '❌ 失败') + (message.error ? ' | ' + message.error : '');
|
||||
console.log(resLog);
|
||||
mainWindow.webContents.send('openclaw-log', { type: message.ok ? 'success' : 'error', message: resLog });
|
||||
}
|
||||
|
||||
// 处理 connect.challenge 质询
|
||||
if (message.type === 'event' && message.event === 'connect.challenge') {
|
||||
console.log('[OpenClaw] Received challenge, sending connect request...');
|
||||
const challengeLog = '[OpenClaw] 🔐 收到 challenge 质询';
|
||||
console.log(challengeLog);
|
||||
mainWindow.webContents.send('openclaw-log', { type: 'info', message: challengeLog });
|
||||
this.sendConnect(message.payload?.nonce);
|
||||
return;
|
||||
}
|
||||
|
||||
this.handleMessage(message);
|
||||
} catch (error) {
|
||||
console.error('[OpenClaw] Parse error:', error);
|
||||
const errorLog = '[OpenClaw] ❌ 解析错误:' + error.message;
|
||||
console.error(errorLog);
|
||||
mainWindow.webContents.send('openclaw-log', { type: 'error', message: errorLog });
|
||||
}
|
||||
});
|
||||
|
||||
this.socket.on('close', () => {
|
||||
this.socket.on('close', (event) => {
|
||||
this.isConnected = false;
|
||||
console.log('[OpenClaw] Disconnected');
|
||||
// 检查窗口是否存在
|
||||
const closeLog = '[OpenClaw] 🔴 连接已关闭 | 代码:' + event.code + ' | 原因:' + (event.reason || '无');
|
||||
console.log(closeLog);
|
||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||
this.eventHandler('disconnected');
|
||||
mainWindow.webContents.send('openclaw-log', { type: 'warning', message: closeLog });
|
||||
}
|
||||
this.scheduleReconnect();
|
||||
});
|
||||
|
||||
this.socket.on('error', (error) => {
|
||||
console.error('[OpenClaw] Error:', error.message);
|
||||
|
||||
// 检测 SSL/TLS 错误并给出建议
|
||||
if (error.message?.includes('WRONG_VERSION_NUMBER')) {
|
||||
console.error('[OpenClaw] ❗ SSL/TLS 协议不匹配!');
|
||||
console.error('[OpenClaw] 建议:');
|
||||
console.error('[OpenClaw] - 如果使用 wss://,请确认服务器支持 SSL');
|
||||
console.error('[OpenClaw] - 如果服务器不支持 SSL,请改用 ws://');
|
||||
console.error('[OpenClaw] - 检查端口是否正确(SSL 和非 SSL 可能使用不同端口)');
|
||||
}
|
||||
|
||||
// 检查窗口是否存在
|
||||
const errorLog = '[OpenClaw] ❌ 错误:' + error.message;
|
||||
console.error(errorLog);
|
||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||
this.eventHandler('error', {
|
||||
error: error.message,
|
||||
hint: error.message?.includes('WRONG_VERSION_NUMBER') ?
|
||||
'SSL 协议不匹配,请检查是否应该使用 ws:// 而不是 wss://' : null
|
||||
});
|
||||
this.eventHandler('error', { error: error.message });
|
||||
mainWindow.webContents.send('openclaw-log', { type: 'error', message: errorLog });
|
||||
if (error.message?.includes('WRONG_VERSION_NUMBER')) {
|
||||
mainWindow.webContents.send('openclaw-log', { type: 'error', message: '🔴 SSL/TLS 协议不匹配!请尝试改用 ws://(非加密)' });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
26
electron/main.js.patch
Normal file
26
electron/main.js.patch
Normal file
@@ -0,0 +1,26 @@
|
||||
--- 这是详细日志的修改说明 ---
|
||||
|
||||
需要在 electron/main.js 的 OpenClawConnection.connect() 方法中:
|
||||
|
||||
1. 连接开始时输出:
|
||||
console.log('='.repeat(60));
|
||||
console.log('[OpenClaw] ========== 开始连接 ==========');
|
||||
console.log('[OpenClaw] 目标地址:', this.url);
|
||||
console.log('[OpenClaw] 协议版本:', this.protocolVersion);
|
||||
|
||||
2. WebSocket open 事件:
|
||||
console.log('[OpenClaw] ✅ WebSocket 连接已建立');
|
||||
console.log('[OpenClaw] 就绪状态:', this.socket.readyState);
|
||||
|
||||
3. 发送 connect 请求:
|
||||
console.log('[OpenClaw] 📤 发送 connect 请求...');
|
||||
this.sendConnect();
|
||||
|
||||
4. 接收消息:
|
||||
console.log('[OpenClaw] 📥 收到消息 (长度:', data.length, '字节)');
|
||||
console.log('[OpenClaw] 原始数据:', data.toString().substring(0, 300));
|
||||
|
||||
5. 连接关闭:
|
||||
console.log('[OpenClaw] 🔴 连接已关闭');
|
||||
console.log('[OpenClaw] 关闭代码:', event.code);
|
||||
console.log('[OpenClaw] 关闭原因:', event.reason);
|
||||
@@ -36,6 +36,11 @@ contextBridge.exposeInMainWorld('electronAPI', {
|
||||
ipcRenderer.on('openclaw-event', (event, data) => callback(data));
|
||||
},
|
||||
|
||||
// OpenClaw 详细日志
|
||||
onOpenClawLog: (callback) => {
|
||||
ipcRenderer.on('openclaw-log', (event, data) => callback(data));
|
||||
},
|
||||
|
||||
// 移除事件监听
|
||||
removeAllListeners: (channel) => {
|
||||
ipcRenderer.removeAllListeners(channel);
|
||||
|
||||
Reference in New Issue
Block a user