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.
 
 

247 lines
9.1 KiB

const WebSocket = require('ws');
// 配置参数
const CONFIG = {
WS_URL: 'ws://localhost:3003', // WebSocket服务器地址
MANAGER_ID: 22, // 客服ID
TIMEOUT: 15000, // 总超时时间
MESSAGE_TIMEOUT: 5000, // 消息响应超时时间
TEST_MODE: 'manager', // 测试模式: 'manager' 或 'user'
DEBUG: true // 启用详细调试日志
};
// 日志函数
function log(...args) {
if (CONFIG.DEBUG) {
console.log(`[${new Date().toLocaleTimeString()}]`, ...args);
}
}
function error(...args) {
console.error(`[${new Date().toLocaleTimeString()}] ERROR:`, ...args);
}
// 测试函数
async function testMessageCenter() {
log('开始测试客服消息中心功能');
log('配置:', CONFIG);
return new Promise((resolve, reject) => {
// 设置总超时
const totalTimeout = setTimeout(() => {
error('测试总超时');
ws.close();
reject(new Error('测试总超时'));
}, CONFIG.TIMEOUT);
// 创建WebSocket连接
let ws;
try {
ws = new WebSocket(CONFIG.WS_URL);
log('正在连接WebSocket服务器...');
} catch (err) {
clearTimeout(totalTimeout);
error('创建WebSocket连接失败:', err.message);
reject(err);
return;
}
// 连接建立
ws.on('open', () => {
log('✅ WebSocket连接已建立');
// 发送认证请求 - 使用兼容格式,包含多种可能的ID字段
const authMessage = {
type: 'auth',
managerId: CONFIG.MANAGER_ID,
userId: CONFIG.MANAGER_ID,
userType: CONFIG.TEST_MODE === 'manager' ? 'manager' : 'user',
data: {
managerId: CONFIG.MANAGER_ID,
userId: CONFIG.MANAGER_ID,
type: CONFIG.TEST_MODE === 'manager' ? 'manager' : 'user',
id: CONFIG.MANAGER_ID
}
};
log('发送认证请求:', authMessage);
ws.send(JSON.stringify(authMessage));
// 等待认证响应
const authResponseTimer = setTimeout(() => {
error('认证响应超时');
ws.close();
clearTimeout(totalTimeout);
reject(new Error('认证响应超时'));
}, CONFIG.MESSAGE_TIMEOUT);
// 消息接收处理
ws.on('message', (data) => {
log('收到消息:', data.toString());
try {
const message = JSON.parse(data.toString());
// 忽略心跳消息
if (message.type === 'heartbeat') {
log('💓 收到心跳消息');
return;
}
// 处理认证成功响应
if (message.type === 'auth_success') {
clearTimeout(authResponseTimer);
log('✅ 认证成功');
// 发送会话列表查询请求 (正确的session格式)
setTimeout(() => {
const listRequest = {
type: 'session',
action: 'list'
};
log('发送会话列表查询请求 (session list格式):', listRequest);
ws.send(JSON.stringify(listRequest));
// 等待会话列表响应
const listResponseTimer = setTimeout(() => {
log('⚠️ 未收到会话列表响应 (list格式),尝试使用另一种格式');
// 尝试使用 type: 'session' + type: 'get_conversations' 格式
const conversationsRequest = {
type: 'session',
data: { type: 'get_conversations' }
};
log('发送会话列表查询请求 (session get_conversations格式):', conversationsRequest);
ws.send(JSON.stringify(conversationsRequest));
// 等待响应
const conversationsTimer = setTimeout(() => {
error('未收到会话列表响应');
ws.close();
clearTimeout(totalTimeout);
reject(new Error('未收到会话列表响应'));
}, CONFIG.MESSAGE_TIMEOUT);
// 监听响应
const handleConversationsResponse = (data) => {
try {
const msg = JSON.parse(data.toString());
// 忽略心跳消息
if (msg.type === 'heartbeat') {
log('💓 收到心跳消息');
return;
}
log('收到后续消息:', msg);
if (msg.type === 'conversations_list' || msg.type === 'session_list') {
clearTimeout(conversationsTimer);
clearTimeout(totalTimeout);
log('✅ 收到会话列表响应:', {
type: msg.type,
hasConversations: msg.data && msg.data.length > 0 || msg.payload && msg.payload.conversations && msg.payload.conversations.length > 0,
conversationCount: msg.data ? msg.data.length : (msg.payload && msg.payload.conversations ? msg.payload.conversations.length : 0)
});
ws.close();
resolve({ success: true, response: msg });
} else if (msg.type === 'error') {
clearTimeout(conversationsTimer);
error('收到错误响应:', msg.message);
ws.close();
clearTimeout(totalTimeout);
reject(new Error(`错误响应: ${msg.message}`));
}
} catch (err) {
error('解析响应失败:', err);
}
};
// 使用on而不是once,因为可能会收到多个心跳消息
ws.on('message', handleConversationsResponse);
}, CONFIG.MESSAGE_TIMEOUT);
}, 1000);
// 第一次响应处理 (专门处理会话列表响应)
const handleSessionListResponse = (data) => {
try {
const msg = JSON.parse(data.toString());
// 忽略心跳消息
if (msg.type === 'heartbeat') {
log('💓 收到心跳消息');
return;
}
log('收到会话列表响应:', msg);
if (msg.type === 'session_list' || msg.type === 'conversations_list') {
clearTimeout(totalTimeout);
log('✅ 收到会话列表响应 (list格式):', {
type: msg.type,
hasConversations: msg.data && msg.data.length > 0 || msg.payload && msg.payload.conversations && msg.payload.conversations.length > 0,
conversationCount: msg.data ? msg.data.length : (msg.payload && msg.payload.conversations ? msg.payload.conversations.length : 0)
});
ws.close();
resolve({ success: true, response: msg });
} else if (msg.type === 'error') {
error('收到错误响应:', msg.message);
ws.close();
clearTimeout(totalTimeout);
reject(new Error(`错误响应: ${msg.message}`));
}
} catch (err) {
error('解析响应失败:', err);
}
};
// 使用on而不是once,因为可能会收到多个心跳消息
ws.on('message', handleSessionListResponse);
}
// 处理认证错误响应
else if (message.type === 'auth_error') {
clearTimeout(authResponseTimer);
error('认证失败:', message.message);
ws.close();
clearTimeout(totalTimeout);
reject(new Error(`认证失败: ${message.message || '未知错误'}`));
}
} catch (err) {
error('解析消息失败:', err, '原始消息:', data.toString());
}
});
});
// 连接错误
ws.on('error', (err) => {
error('WebSocket错误:', err.message);
clearTimeout(totalTimeout);
reject(err);
});
// 连接关闭
ws.on('close', (code, reason) => {
log('WebSocket连接已关闭:', { code, reason: reason.toString() });
});
});
}
// 运行测试
console.log('========================================');
console.log('客服消息中心功能测试');
console.log('========================================');
testMessageCenter()
.then(result => {
console.log('\n========================================');
console.log('✅ 测试成功');
console.log('========================================');
process.exit(0);
})
.catch(error => {
console.log('\n========================================');
console.error('❌ 测试失败:', error.message);
console.log('========================================');
process.exit(1);
});