// 简化的客服在线状态测试脚本 const WebSocket = require('ws'); const http = require('http'); // 服务器地址 const SERVER_URL = 'ws://localhost:3003'; const API_URL = 'http://localhost:3003/api/managers'; // 客服信息 const customerServicePhone = '17780155537'; const customerServiceId = '22'; // 刘杨的ID console.log('=== 简化的客服在线状态测试 ==='); console.log(`测试客服ID: ${customerServiceId}`); // 检查客服当前状态 function checkStatus() { return new Promise((resolve, reject) => { http.get(API_URL, (res) => { let data = ''; res.on('data', chunk => data += chunk); res.on('end', () => { try { const result = JSON.parse(data); const manager = result.data.find(m => m.managerId === customerServiceId); if (manager) { console.log(`客服状态: ${manager.online ? '✅ 在线' : '❌ 离线'}`); resolve(manager.online); } else { console.log('未找到客服信息'); resolve(false); } } catch (e) { reject(e); } }); }).on('error', reject); }); } // 使用基础认证格式(与诊断脚本中成功的格式一致) function testSimpleAuth() { return new Promise((resolve, reject) => { console.log('\n--- 使用基础认证格式 ---'); const ws = new WebSocket(SERVER_URL); ws.on('open', () => { console.log('连接已建立'); // 使用最简单的认证格式 const authMessage = { managerId: customerServiceId, type: 'manager' }; console.log('发送认证:', JSON.stringify(authMessage)); ws.send(JSON.stringify(authMessage)); }); ws.on('message', (data) => { const msg = JSON.parse(data); console.log('收到响应:', msg); if (msg.type === 'auth_success') { console.log('✅ 认证成功!'); resolve({ success: true, ws }); } else { console.log('❌ 认证失败'); ws.close(); resolve({ success: false }); } }); ws.on('error', (e) => { console.log('连接错误:', e.message); resolve({ success: false }); }); setTimeout(() => { console.log('连接超时'); ws.close(); resolve({ success: false }); }, 5000); }); } // 主函数 async function runTest() { try { // 1. 检查初始状态 console.log('\n1. 检查初始状态:'); await checkStatus(); // 2. 尝试认证 console.log('\n2. 尝试WebSocket认证:'); const authResult = await testSimpleAuth(); if (authResult.success) { // 3. 等待3秒后再次检查状态 console.log('\n3. 等待并检查更新后的状态...'); setTimeout(async () => { const newStatus = await checkStatus(); console.log('\n=== 测试结果 ==='); if (newStatus) { console.log('🎉 成功!客服状态已更新为在线'); } else { console.log('❌ 失败!客服仍显示离线'); } // 保持连接15秒 console.log('\n保持连接15秒,请在前端查看状态...'); setTimeout(() => { authResult.ws.close(); console.log('\n测试完成!'); }, 15000); }, 3000); } else { console.log('\n=== 测试结果 ==='); console.log('❌ 认证失败,请检查认证格式或服务器配置'); } } catch (error) { console.error('测试过程中出错:', error); } } runTest();