// 客服在线状态完整测试脚本 const WebSocket = require('ws'); const serverUrl = 'ws://localhost:3003'; const customerServiceId = '22'; console.log('=== 客服在线状态完整测试 ==='); console.log(`连接服务器: ${serverUrl}`); console.log(`客服ID: ${customerServiceId}`); console.log('=== 开始测试 ==='); // 创建WebSocket连接 const ws = new WebSocket(serverUrl); // 连接成功事件 ws.on('open', () => { console.log('✅ WebSocket连接已建立'); // 发送正确格式的认证消息 const authMessage = { type: 'auth', // 必须是'auth'才能被服务器识别为认证消息 managerId: customerServiceId, userType: 'manager' // 使用userType表示用户类型 }; const messageString = JSON.stringify(authMessage); console.log(`📤 发送认证消息: ${messageString}`); ws.send(messageString); }); // 接收消息事件 ws.on('message', (data) => { try { const message = JSON.parse(data.toString()); console.log('📥 收到服务器消息:', JSON.stringify(message, null, 2)); if (message.type === 'auth_success') { console.log('🎉 认证成功!客服应该已在线'); console.log(`📊 认证信息:`, message.payload); console.log('✅ 测试成功: 客服已成功设置为在线状态'); // 延迟2秒后关闭连接 setTimeout(() => { console.log('🔄 关闭测试连接'); ws.close(); }, 2000); } else if (message.type === 'auth_error') { console.error('❌ 认证失败:', message.message); } } catch (error) { console.error('❌ 解析消息失败:', error); } }); // 连接关闭事件 ws.on('close', (code, reason) => { console.log(`❌ WebSocket连接已关闭`); console.log(` 关闭代码: ${code}`); console.log(` 关闭原因: ${reason}`); console.log('=== 测试结束 ==='); }); // 连接错误事件 ws.on('error', (error) => { console.error('❌ WebSocket连接错误:', error.message); }); // 设置超时 setTimeout(() => { if (ws.readyState === WebSocket.OPEN) { console.error('⏰ 30秒超时,测试失败'); ws.close(); } }, 30000);