You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
252 lines
6.9 KiB
252 lines
6.9 KiB
// 客服连接状态诊断工具
|
|
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(`测试客服: 手机号 ${customerServicePhone}, ID ${customerServiceId}`);
|
|
|
|
// 1. 首先检查API中的客服状态
|
|
function checkCurrentStatus() {
|
|
return new Promise((resolve, reject) => {
|
|
console.log('\n📊 检查API中的客服当前状态...');
|
|
http.get(API_URL, (res) => {
|
|
let data = '';
|
|
|
|
res.on('data', (chunk) => {
|
|
data += chunk;
|
|
});
|
|
|
|
res.on('end', () => {
|
|
try {
|
|
const result = JSON.parse(data);
|
|
const targetManager = result.data.find(m => m.managerId === customerServiceId || m.phoneNumber === customerServicePhone);
|
|
|
|
if (targetManager) {
|
|
console.log(`✅ 找到目标客服: ${targetManager.name}`);
|
|
console.log(` 当前在线状态: ${targetManager.online ? '在线' : '离线'}`);
|
|
console.log(` 完整信息:`, targetManager);
|
|
} else {
|
|
console.log('❌ 未找到目标客服信息');
|
|
}
|
|
|
|
resolve(result);
|
|
} catch (e) {
|
|
console.error('❌ 解析API响应失败:', e);
|
|
reject(e);
|
|
}
|
|
});
|
|
}).on('error', (error) => {
|
|
console.error('❌ API请求失败:', error);
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|
|
|
|
// 2. 测试不同格式的WebSocket认证消息
|
|
async function testWebSocketAuthentication() {
|
|
console.log('\n🔄 测试WebSocket认证(模拟用户登录后连接)...');
|
|
|
|
// 测试多种可能的认证格式
|
|
const authFormats = [
|
|
{
|
|
name: '格式1: 直接认证格式',
|
|
message: {
|
|
type: 'auth',
|
|
managerId: customerServiceId,
|
|
type: 'manager',
|
|
name: '刘杨'
|
|
}
|
|
},
|
|
{
|
|
name: '格式2: 嵌套data对象',
|
|
message: {
|
|
type: 'auth',
|
|
data: {
|
|
managerId: customerServiceId,
|
|
type: 'manager',
|
|
name: '刘杨'
|
|
}
|
|
}
|
|
},
|
|
{
|
|
name: '格式3: 双重嵌套data对象',
|
|
message: {
|
|
type: 'auth',
|
|
data: {
|
|
data: {
|
|
managerId: customerServiceId,
|
|
type: 'manager',
|
|
name: '刘杨'
|
|
}
|
|
}
|
|
}
|
|
},
|
|
{
|
|
name: '格式4: 包含手机号',
|
|
message: {
|
|
type: 'auth',
|
|
data: {
|
|
managerId: customerServiceId,
|
|
phoneNumber: customerServicePhone,
|
|
type: 'manager',
|
|
name: '刘杨'
|
|
}
|
|
}
|
|
}
|
|
];
|
|
|
|
// 依次测试每种格式
|
|
for (const format of authFormats) {
|
|
await new Promise((resolve) => {
|
|
console.log(`\n${format.name}:`);
|
|
console.log(`发送认证消息:`, JSON.stringify(format.message));
|
|
|
|
const ws = new WebSocket(SERVER_URL);
|
|
let responseReceived = false;
|
|
|
|
ws.on('open', () => {
|
|
ws.send(JSON.stringify(format.message));
|
|
|
|
// 设置超时
|
|
setTimeout(() => {
|
|
if (!responseReceived) {
|
|
console.log('❌ 未收到响应(超时)');
|
|
ws.close();
|
|
resolve();
|
|
}
|
|
}, 3000);
|
|
});
|
|
|
|
ws.on('message', (data) => {
|
|
responseReceived = true;
|
|
try {
|
|
const message = JSON.parse(data.toString());
|
|
console.log('✅ 收到响应:', message);
|
|
|
|
if (message.type === 'auth_success') {
|
|
console.log('✅ 认证成功!');
|
|
} else if (message.type === 'auth_error') {
|
|
console.log('❌ 认证失败:', message.message || '未知错误');
|
|
}
|
|
|
|
ws.close();
|
|
resolve();
|
|
} catch (e) {
|
|
console.error('❌ 解析响应失败:', e);
|
|
ws.close();
|
|
resolve();
|
|
}
|
|
});
|
|
|
|
ws.on('error', (error) => {
|
|
responseReceived = true;
|
|
console.error('❌ WebSocket错误:', error.message);
|
|
resolve();
|
|
});
|
|
});
|
|
|
|
// 等待1秒后测试下一种格式
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
}
|
|
|
|
console.log('\n🔍 诊断完成!请检查上面的测试结果。');
|
|
}
|
|
|
|
// 3. 模拟完整的登录-连接流程
|
|
async function simulateCompleteLoginFlow() {
|
|
console.log('\n🎯 模拟完整的登录-连接流程...');
|
|
|
|
// 先检查初始状态
|
|
await checkCurrentStatus();
|
|
|
|
// 进行WebSocket连接和认证
|
|
const ws = new WebSocket(SERVER_URL);
|
|
|
|
return new Promise((resolve) => {
|
|
ws.on('open', () => {
|
|
console.log('✅ WebSocket连接已建立');
|
|
|
|
// 使用最可能成功的格式(嵌套data对象)
|
|
const authMessage = {
|
|
type: 'auth',
|
|
data: {
|
|
managerId: customerServiceId,
|
|
phoneNumber: customerServicePhone,
|
|
type: 'manager',
|
|
name: '刘杨'
|
|
}
|
|
};
|
|
|
|
console.log('发送认证消息:', JSON.stringify(authMessage));
|
|
ws.send(JSON.stringify(authMessage));
|
|
});
|
|
|
|
ws.on('message', async (data) => {
|
|
try {
|
|
const message = JSON.parse(data.toString());
|
|
console.log('收到响应:', message);
|
|
|
|
if (message.type === 'auth_success') {
|
|
console.log('✅ 认证成功!等待2秒后检查状态...');
|
|
|
|
// 等待2秒后再次检查状态
|
|
setTimeout(async () => {
|
|
await checkCurrentStatus();
|
|
ws.close();
|
|
resolve();
|
|
}, 2000);
|
|
} else if (message.type === 'auth_error') {
|
|
console.log('❌ 认证失败:', message.message || '未知错误');
|
|
ws.close();
|
|
resolve();
|
|
}
|
|
} catch (e) {
|
|
console.error('❌ 解析消息失败:', e);
|
|
ws.close();
|
|
resolve();
|
|
}
|
|
});
|
|
|
|
ws.on('error', (error) => {
|
|
console.error('❌ WebSocket错误:', error);
|
|
ws.close();
|
|
resolve();
|
|
});
|
|
});
|
|
}
|
|
|
|
// 主诊断函数
|
|
async function runDiagnostics() {
|
|
try {
|
|
console.log('====================================');
|
|
console.log(' 客服连接状态诊断工具');
|
|
console.log('====================================');
|
|
|
|
// 1. 检查当前API状态
|
|
await checkCurrentStatus();
|
|
|
|
// 2. 测试不同认证格式
|
|
await testWebSocketAuthentication();
|
|
|
|
// 3. 模拟完整流程
|
|
await simulateCompleteLoginFlow();
|
|
|
|
console.log('\n====================================');
|
|
console.log('诊断完成!根据测试结果分析问题原因。');
|
|
console.log('====================================');
|
|
|
|
} catch (error) {
|
|
console.error('\n❌ 诊断过程中出现错误:', error);
|
|
}
|
|
}
|
|
|
|
// 运行诊断
|
|
runDiagnostics();
|