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.
143 lines
4.4 KiB
143 lines
4.4 KiB
|
3 months ago
|
// 简化的客服消息中心测试脚本
|
||
|
|
const WebSocket = require('ws');
|
||
|
|
|
||
|
|
// 服务器配置
|
||
|
|
const SERVER_URL = 'ws://localhost:3003';
|
||
|
|
const TEST_MANAGER_ID = '22'; // 客服ID
|
||
|
|
const TEST_CONVERSATION_ID = '963f9eed-950c-47e9-ade6-97e7e90915dc'; // 测试会话ID
|
||
|
|
|
||
|
|
console.log(`=== 启动客服消息中心功能测试 ===`);
|
||
|
|
console.log(`连接到: ${SERVER_URL}`);
|
||
|
|
console.log(`客服ID: ${TEST_MANAGER_ID}`);
|
||
|
|
console.log(`测试会话ID: ${TEST_CONVERSATION_ID}`);
|
||
|
|
|
||
|
|
// 创建WebSocket连接
|
||
|
|
const ws = new WebSocket(SERVER_URL);
|
||
|
|
|
||
|
|
// 连接成功
|
||
|
|
ws.on('open', () => {
|
||
|
|
console.log('✅ WebSocket连接已建立');
|
||
|
|
|
||
|
|
// 发送认证消息
|
||
|
|
const authMessage = {
|
||
|
|
type: 'auth',
|
||
|
|
managerId: TEST_MANAGER_ID,
|
||
|
|
userType: 'manager'
|
||
|
|
};
|
||
|
|
|
||
|
|
console.log('🔑 发送认证消息:', JSON.stringify(authMessage));
|
||
|
|
ws.send(JSON.stringify(authMessage));
|
||
|
|
});
|
||
|
|
|
||
|
|
// 接收消息
|
||
|
|
ws.on('message', (data) => {
|
||
|
|
try {
|
||
|
|
const message = JSON.parse(data.toString());
|
||
|
|
console.log('📥 收到消息:', JSON.stringify(message));
|
||
|
|
|
||
|
|
// 处理认证成功
|
||
|
|
if (message.type === 'auth_success') {
|
||
|
|
console.log('✅ 客服认证成功');
|
||
|
|
|
||
|
|
// 认证成功后,尝试获取消息列表
|
||
|
|
console.log('\n🔍 尝试获取消息列表...');
|
||
|
|
|
||
|
|
// 尝试多种消息查询格式
|
||
|
|
const queryFormats = [
|
||
|
|
// 格式1: 简单查询
|
||
|
|
{
|
||
|
|
type: 'query_chat_list',
|
||
|
|
managerId: TEST_MANAGER_ID
|
||
|
|
},
|
||
|
|
// 格式2: 直接查询特定会话
|
||
|
|
{
|
||
|
|
type: 'get_conversation_messages',
|
||
|
|
conversationId: TEST_CONVERSATION_ID,
|
||
|
|
managerId: TEST_MANAGER_ID
|
||
|
|
}
|
||
|
|
];
|
||
|
|
|
||
|
|
// 逐一发送查询
|
||
|
|
queryFormats.forEach((format, index) => {
|
||
|
|
setTimeout(() => {
|
||
|
|
console.log(`\n尝试查询格式 ${index + 1}:`, JSON.stringify(format));
|
||
|
|
ws.send(JSON.stringify(format));
|
||
|
|
}, index * 2000);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// 处理消息列表响应
|
||
|
|
else if (message.type === 'chat_list' || message.type === 'message_list' || message.type === 'conversation_messages') {
|
||
|
|
console.log('\n🎉 成功接收到消息列表/会话消息!');
|
||
|
|
console.log('响应详情:', JSON.stringify(message, null, 2));
|
||
|
|
|
||
|
|
// 检查是否包含我们的测试会话
|
||
|
|
const dataArray = message.data || message.messages || message.payload || [];
|
||
|
|
if (Array.isArray(dataArray)) {
|
||
|
|
console.log(`\n📋 共收到 ${dataArray.length} 条记录`);
|
||
|
|
|
||
|
|
// 查找特定会话
|
||
|
|
const targetConversation = dataArray.find(item =>
|
||
|
|
item.conversationId === TEST_CONVERSATION_ID ||
|
||
|
|
item.id === TEST_CONVERSATION_ID ||
|
||
|
|
item.conversation_id === TEST_CONVERSATION_ID
|
||
|
|
);
|
||
|
|
|
||
|
|
if (targetConversation) {
|
||
|
|
console.log('\n✅ 找到测试会话!');
|
||
|
|
console.log('会话信息:', JSON.stringify(targetConversation, null, 2));
|
||
|
|
} else {
|
||
|
|
console.log('\n📋 所有会话记录:');
|
||
|
|
dataArray.forEach((item, i) => {
|
||
|
|
console.log(`\n--- 会话 ${i + 1} ---`);
|
||
|
|
console.log(`会话ID: ${item.conversationId || item.id || item.conversation_id || '未知'}`);
|
||
|
|
if (item.lastMessage || item.last_message) {
|
||
|
|
console.log(`最后消息: ${item.lastMessage || item.last_message}`);
|
||
|
|
}
|
||
|
|
if (item.unreadCount || item.unread_count) {
|
||
|
|
console.log(`未读数: ${item.unreadCount || item.unread_count}`);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 处理错误
|
||
|
|
else if (message.type === 'error') {
|
||
|
|
console.error('❌ 收到错误消息:', message.message);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 处理心跳
|
||
|
|
else if (message.type === 'heartbeat') {
|
||
|
|
console.log('💓 收到心跳消息');
|
||
|
|
// 回复心跳以保持连接
|
||
|
|
ws.send(JSON.stringify({ type: 'heartbeat_response' }));
|
||
|
|
}
|
||
|
|
|
||
|
|
// 其他消息类型
|
||
|
|
else {
|
||
|
|
console.log('📝 收到其他类型消息:', message.type);
|
||
|
|
}
|
||
|
|
} catch (e) {
|
||
|
|
console.error('❌ 解析消息失败:', e);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// 连接错误
|
||
|
|
ws.on('error', (error) => {
|
||
|
|
console.error('❌ WebSocket连接错误:', error);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 连接关闭
|
||
|
|
ws.on('close', () => {
|
||
|
|
console.log('🔌 WebSocket连接已关闭');
|
||
|
|
});
|
||
|
|
|
||
|
|
// 设置超时
|
||
|
|
setTimeout(() => {
|
||
|
|
console.log('\n⏰ 测试完成,关闭连接');
|
||
|
|
if (ws.readyState === WebSocket.OPEN) {
|
||
|
|
ws.close();
|
||
|
|
}
|
||
|
|
}, 30000);
|