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.

113 lines
3.7 KiB

// 综合验证脚本:测试客服在线状态认证和同步
const WebSocket = require('ws');
const readline = require('readline');
// 创建命令行交互接口
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// 模拟客服ID列表
const TEST_MANAGER_ID = '22'; // 测试用客服ID
const WEBSOCKET_URL = 'ws://localhost:3003';
// 模拟客服认证消息
function createManagerAuthMessage(managerId) {
return {
type: 'auth',
managerId: managerId,
userType: 'manager',
timestamp: Date.now()
};
}
// 执行完整的验证流程
async function runCompleteVerification() {
console.log('==========================================');
console.log('客服在线状态综合验证测试');
console.log('==========================================');
// 1. 建立WebSocket连接
console.log(`\n[步骤1] 正在连接WebSocket服务器: ${WEBSOCKET_URL}`);
const ws = new WebSocket(WEBSOCKET_URL);
ws.on('open', () => {
console.log('[成功] WebSocket连接已建立');
// 2. 发送客服认证消息
console.log(`\n[步骤2] 发送客服认证消息,managerId: ${TEST_MANAGER_ID}`);
const authMessage = createManagerAuthMessage(TEST_MANAGER_ID);
console.log('发送的认证消息:', JSON.stringify(authMessage));
ws.send(JSON.stringify(authMessage));
});
// 3. 监听认证响应
ws.on('message', (data) => {
try {
const response = JSON.parse(data);
console.log('\n[接收到服务器响应]:', JSON.stringify(response));
if (response.type === 'auth_success') {
console.log('[成功] 客服认证成功!');
console.log('\n[验证结果]:');
console.log(`- 认证状态: 成功`);
console.log(`- 客服ID: ${TEST_MANAGER_ID}`);
console.log(`- 用户类型: ${response.payload?.type || '未知'}`);
// 5. 说明下一步操作
console.log('\n==========================================');
console.log('验证完成!');
console.log('请前往服务器日志查看:');
console.log('1. 客服认证是否成功');
console.log('2. 客服在线状态是否正确更新');
console.log('3. isManagerOnline函数是否返回true');
console.log('==========================================');
// 保持连接一段时间以便观察心跳
setTimeout(() => {
console.log('\n验证连接将在5秒后关闭...');
setTimeout(() => {
ws.close();
rl.close();
}, 5000);
}, 1000);
} else if (response.type === 'auth_error') {
console.error('[失败] 客服认证失败:', response.message);
ws.close();
rl.close();
} else if (response.type === 'heartbeat') {
console.log('[状态] 收到心跳消息,连接保持活跃');
// 发送心跳响应(如果需要)
ws.send(JSON.stringify({ type: 'pong' }));
}
} catch (error) {
console.error('[错误] 解析服务器响应失败:', error);
}
});
// 4. 监听错误和关闭事件
ws.on('error', (error) => {
console.error('[错误] WebSocket连接错误:', error.message);
});
ws.on('close', (code, reason) => {
console.log(`[状态] WebSocket连接已关闭,代码: ${code}, 原因: ${reason || '正常关闭'}`);
console.log('\n测试结束。请检查服务器日志以确认客服在线状态是否正确更新。');
});
}
// 开始验证
runCompleteVerification().catch(err => {
console.error('验证过程中发生错误:', err);
rl.close();
});
// 处理用户中断
rl.on('SIGINT', () => {
console.log('\n验证已取消');
rl.close();
});