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.
122 lines
3.2 KiB
122 lines
3.2 KiB
|
3 months ago
|
// 简化的聊天功能调试脚本
|
||
|
|
const WebSocket = require('ws');
|
||
|
|
|
||
|
|
// 配置信息
|
||
|
|
const WS_URL = 'ws://localhost:3003';
|
||
|
|
const TEST_MANAGER_ID = '22';
|
||
|
|
const TEST_CUSTOMER_ID = 'test_customer_1';
|
||
|
|
|
||
|
|
// 创建客服WebSocket连接
|
||
|
|
function createManagerConnection() {
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
const ws = new WebSocket(WS_URL);
|
||
|
|
|
||
|
|
ws.on('open', () => {
|
||
|
|
console.log('客服WebSocket连接已建立');
|
||
|
|
// 发送认证消息
|
||
|
|
const authMsg = JSON.stringify({
|
||
|
|
type: 'auth',
|
||
|
|
managerId: TEST_MANAGER_ID,
|
||
|
|
userType: 'manager'
|
||
|
|
});
|
||
|
|
console.log('客服发送认证消息:', authMsg);
|
||
|
|
ws.send(authMsg);
|
||
|
|
});
|
||
|
|
|
||
|
|
ws.on('message', (data) => {
|
||
|
|
const message = JSON.parse(data.toString());
|
||
|
|
console.log('客服收到消息:', message);
|
||
|
|
|
||
|
|
if (message.type === 'auth_success') {
|
||
|
|
console.log('客服认证成功');
|
||
|
|
resolve(ws);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
ws.on('error', (error) => {
|
||
|
|
console.error('客服WebSocket错误:', error);
|
||
|
|
reject(error);
|
||
|
|
});
|
||
|
|
|
||
|
|
ws.on('close', () => {
|
||
|
|
console.log('客服WebSocket连接已关闭');
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// 测试客服发送消息
|
||
|
|
async function testManagerSendMessage(managerWs, conversationId) {
|
||
|
|
return new Promise((resolve) => {
|
||
|
|
const replyMessage = '这是客服的测试回复';
|
||
|
|
const messageId = 'debug_reply_' + Date.now();
|
||
|
|
|
||
|
|
// 简化的消息格式,只包含最基本的字段
|
||
|
|
const replyData = {
|
||
|
|
type: 'chat_message',
|
||
|
|
conversationId: conversationId,
|
||
|
|
messageId: messageId,
|
||
|
|
content: replyMessage,
|
||
|
|
contentType: 1,
|
||
|
|
senderType: 2,
|
||
|
|
senderId: TEST_MANAGER_ID,
|
||
|
|
receiverId: TEST_CUSTOMER_ID
|
||
|
|
};
|
||
|
|
|
||
|
|
console.log('客服准备发送消息:', replyData);
|
||
|
|
managerWs.send(JSON.stringify(replyData));
|
||
|
|
|
||
|
|
// 设置消息接收处理
|
||
|
|
const messageHandler = (data) => {
|
||
|
|
const message = JSON.parse(data.toString());
|
||
|
|
console.log('客服收到响应:', message);
|
||
|
|
|
||
|
|
if (message.type === 'error') {
|
||
|
|
console.error('发送失败:', message.message);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 无论成功失败,5秒后解析
|
||
|
|
setTimeout(() => {
|
||
|
|
managerWs.removeListener('message', messageHandler);
|
||
|
|
resolve(message);
|
||
|
|
}, 5000);
|
||
|
|
};
|
||
|
|
|
||
|
|
managerWs.on('message', messageHandler);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// 主测试函数
|
||
|
|
async function runTest() {
|
||
|
|
try {
|
||
|
|
// 使用已知的会话ID (从之前的测试中获取)
|
||
|
|
const conversationId = '4fa4b92f-df20-40ae-94b9-f906753a4cfd';
|
||
|
|
|
||
|
|
console.log('=== 开始调试客服发送消息功能 ===');
|
||
|
|
console.log('使用会话ID:', conversationId);
|
||
|
|
|
||
|
|
// 创建客服连接
|
||
|
|
const managerWs = await createManagerConnection();
|
||
|
|
|
||
|
|
// 等待2秒确保连接稳定
|
||
|
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
||
|
|
|
||
|
|
// 测试发送消息
|
||
|
|
console.log('\n测试客服发送消息...');
|
||
|
|
const response = await testManagerSendMessage(managerWs, conversationId);
|
||
|
|
|
||
|
|
console.log('\n=== 测试完成 ===');
|
||
|
|
console.log('响应结果:', response);
|
||
|
|
|
||
|
|
// 关闭连接
|
||
|
|
setTimeout(() => {
|
||
|
|
managerWs.close();
|
||
|
|
}, 1000);
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('测试失败:', error);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 运行测试
|
||
|
|
runTest();
|