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.
193 lines
5.3 KiB
193 lines
5.3 KiB
// 完整聊天流程调试脚本
|
|
const WebSocket = require('ws');
|
|
|
|
const WS_URL = 'ws://localhost:3003';
|
|
const TEST_MANAGER_ID = '22';
|
|
const TEST_CUSTOMER_ID = 'test_customer_1';
|
|
let customerWs = null;
|
|
let managerWs = null;
|
|
let currentConversationId = null;
|
|
|
|
// 启动调试
|
|
async function startDebug() {
|
|
console.log('=== 启动完整聊天流程调试 ===');
|
|
|
|
try {
|
|
// 连接客服WebSocket
|
|
await connectManager();
|
|
|
|
// 连接客户WebSocket
|
|
await connectCustomer();
|
|
|
|
// 等待连接稳定
|
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
|
|
// 客户发送消息
|
|
await customerSendMessage();
|
|
|
|
// 等待客服收到消息
|
|
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
|
|
// 如果会话ID有效,客服回复消息
|
|
if (currentConversationId) {
|
|
await managerReplyMessage(currentConversationId);
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('❌ 调试过程中出现错误:', error);
|
|
} finally {
|
|
console.log('=== 调试结束 ===');
|
|
if (customerWs && customerWs.readyState === WebSocket.OPEN) {
|
|
customerWs.close();
|
|
}
|
|
if (managerWs && managerWs.readyState === WebSocket.OPEN) {
|
|
managerWs.close();
|
|
}
|
|
}
|
|
}
|
|
|
|
// 连接客服WebSocket
|
|
function connectManager() {
|
|
return new Promise((resolve, reject) => {
|
|
managerWs = new WebSocket(WS_URL);
|
|
|
|
managerWs.on('open', () => {
|
|
console.log('✅ 客服WebSocket连接已建立');
|
|
|
|
// 发送认证消息
|
|
const authMsg = JSON.stringify({
|
|
type: 'auth',
|
|
managerId: TEST_MANAGER_ID,
|
|
userType: 'manager'
|
|
});
|
|
managerWs.send(authMsg);
|
|
});
|
|
|
|
managerWs.on('message', (data) => {
|
|
const message = JSON.parse(data.toString());
|
|
console.log('📥 客服收到消息:', JSON.stringify(message));
|
|
|
|
if (message.type === 'auth_success') {
|
|
console.log('✅ 客服认证成功');
|
|
resolve();
|
|
} else if (message.type === 'new_message' && message.payload) {
|
|
// 记录会话ID
|
|
if (message.payload.conversationId) {
|
|
currentConversationId = message.payload.conversationId;
|
|
console.log(`📝 获取到会话ID: ${currentConversationId}`);
|
|
}
|
|
}
|
|
});
|
|
|
|
managerWs.on('error', (error) => {
|
|
console.error('❌ 客服WebSocket错误:', error);
|
|
reject(error);
|
|
});
|
|
|
|
setTimeout(() => {
|
|
reject(new Error('客服连接或认证超时'));
|
|
}, 5000);
|
|
});
|
|
}
|
|
|
|
// 连接客户WebSocket
|
|
function connectCustomer() {
|
|
return new Promise((resolve, reject) => {
|
|
customerWs = new WebSocket(WS_URL);
|
|
|
|
customerWs.on('open', () => {
|
|
console.log('✅ 客户WebSocket连接已建立');
|
|
|
|
// 发送认证消息
|
|
const authMsg = JSON.stringify({
|
|
type: 'auth',
|
|
userId: TEST_CUSTOMER_ID,
|
|
userType: 'user'
|
|
});
|
|
customerWs.send(authMsg);
|
|
});
|
|
|
|
customerWs.on('message', (data) => {
|
|
const message = JSON.parse(data.toString());
|
|
console.log('📥 客户收到消息:', JSON.stringify(message));
|
|
|
|
if (message.type === 'auth_success') {
|
|
console.log('✅ 客户认证成功');
|
|
resolve();
|
|
}
|
|
});
|
|
|
|
customerWs.on('error', (error) => {
|
|
console.error('❌ 客户WebSocket错误:', error);
|
|
reject(error);
|
|
});
|
|
|
|
setTimeout(() => {
|
|
reject(new Error('客户连接或认证超时'));
|
|
}, 5000);
|
|
});
|
|
}
|
|
|
|
// 客户发送消息
|
|
function customerSendMessage() {
|
|
return new Promise((resolve, reject) => {
|
|
const messageId = 'test_customer_' + Date.now();
|
|
const message = {
|
|
type: 'chat_message',
|
|
payload: {
|
|
messageId: messageId,
|
|
managerId: TEST_MANAGER_ID,
|
|
content: '你好,我是测试客户,有问题咨询',
|
|
contentType: 1
|
|
}
|
|
};
|
|
|
|
console.log('📤 客户发送消息:', JSON.stringify(message));
|
|
customerWs.send(JSON.stringify(message));
|
|
|
|
// 等待发送确认
|
|
const messageHandler = (data) => {
|
|
const response = JSON.parse(data.toString());
|
|
if (response.type === 'message_sent' && response.payload.messageId === messageId) {
|
|
customerWs.off('message', messageHandler);
|
|
console.log('✅ 客户消息发送成功');
|
|
resolve();
|
|
}
|
|
};
|
|
|
|
customerWs.on('message', messageHandler);
|
|
|
|
setTimeout(() => {
|
|
customerWs.off('message', messageHandler);
|
|
reject(new Error('客户消息发送超时'));
|
|
}, 5000);
|
|
});
|
|
}
|
|
|
|
// 客服回复消息
|
|
function managerReplyMessage(conversationId) {
|
|
return new Promise((resolve, reject) => {
|
|
const messageId = 'test_manager_' + Date.now();
|
|
// 尝试使用更简单的格式,只包含最基本的字段
|
|
// 参考客户发送消息的格式,但使用conversationId而不是managerId
|
|
const replyMessage = {
|
|
type: 'chat_message',
|
|
payload: {
|
|
content: '您好,我是客服,请问有什么可以帮助您的?', // 必须字段
|
|
conversationId: conversationId, // 必须字段,确定会话
|
|
contentType: 1 // 必须字段
|
|
}
|
|
};
|
|
|
|
console.log('📤 客服发送回复消息:', JSON.stringify(replyMessage));
|
|
managerWs.send(JSON.stringify(replyMessage));
|
|
|
|
// 设置超时
|
|
setTimeout(() => {
|
|
resolve();
|
|
}, 3000);
|
|
});
|
|
}
|
|
|
|
// 启动调试
|
|
startDebug();
|