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.
244 lines
6.8 KiB
244 lines
6.8 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';
|
||
|
|
let customerWs = null;
|
||
|
|
let managerWs = null;
|
||
|
|
let conversationId = null;
|
||
|
|
let messageSent = false;
|
||
|
|
|
||
|
|
// 启动调试
|
||
|
|
async function startDebug() {
|
||
|
|
console.log('=== 启动新会话调试 ===');
|
||
|
|
|
||
|
|
try {
|
||
|
|
// 连接客服WebSocket
|
||
|
|
await connectManager();
|
||
|
|
|
||
|
|
// 等待一会儿
|
||
|
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
||
|
|
|
||
|
|
// 连接客户WebSocket
|
||
|
|
await connectCustomer();
|
||
|
|
|
||
|
|
// 等待一会儿
|
||
|
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
||
|
|
|
||
|
|
// 客户发送消息
|
||
|
|
await customerSendMessage();
|
||
|
|
|
||
|
|
// 等待客服收到消息并获取会话ID
|
||
|
|
await new Promise((resolve, reject) => {
|
||
|
|
const timeout = setTimeout(() => {
|
||
|
|
reject(new Error('等待会话ID超时'));
|
||
|
|
}, 10000);
|
||
|
|
|
||
|
|
const checkConversationId = setInterval(() => {
|
||
|
|
if (conversationId && !messageSent) {
|
||
|
|
clearInterval(checkConversationId);
|
||
|
|
clearTimeout(timeout);
|
||
|
|
resolve();
|
||
|
|
}
|
||
|
|
}, 500);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 客服回复消息
|
||
|
|
if (conversationId) {
|
||
|
|
await managerReplyMessage();
|
||
|
|
}
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('❌ 调试过程中出现错误:', error);
|
||
|
|
} finally {
|
||
|
|
console.log('=== 调试结束 ===');
|
||
|
|
cleanup();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 连接客服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'
|
||
|
|
});
|
||
|
|
console.log('📤 客服发送认证消息:', authMsg);
|
||
|
|
managerWs.send(authMsg);
|
||
|
|
});
|
||
|
|
|
||
|
|
managerWs.on('message', (data) => {
|
||
|
|
try {
|
||
|
|
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') {
|
||
|
|
// 获取会话ID
|
||
|
|
if (message.payload && message.payload.conversationId) {
|
||
|
|
conversationId = message.payload.conversationId;
|
||
|
|
console.log(`📝 获取到新会话ID: ${conversationId}`);
|
||
|
|
}
|
||
|
|
} else if (message.type === 'error') {
|
||
|
|
console.error('❌ 客服收到错误:', message.message);
|
||
|
|
}
|
||
|
|
} catch (e) {
|
||
|
|
console.error('❌ 解析客服消息失败:', e);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
managerWs.on('error', (error) => {
|
||
|
|
console.error('❌ 客服WebSocket错误:', error);
|
||
|
|
reject(error);
|
||
|
|
});
|
||
|
|
|
||
|
|
managerWs.on('close', () => {
|
||
|
|
console.log('❌ 客服WebSocket连接已关闭');
|
||
|
|
});
|
||
|
|
|
||
|
|
setTimeout(() => {
|
||
|
|
reject(new Error('客服连接或认证超时'));
|
||
|
|
}, 10000);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// 连接客户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'
|
||
|
|
});
|
||
|
|
console.log('📤 客户发送认证消息:', authMsg);
|
||
|
|
customerWs.send(authMsg);
|
||
|
|
});
|
||
|
|
|
||
|
|
customerWs.on('message', (data) => {
|
||
|
|
try {
|
||
|
|
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') {
|
||
|
|
// 客户收到新消息(可能是客服回复)
|
||
|
|
console.log('✅ 客户收到客服回复:', message.payload?.content);
|
||
|
|
} else if (message.type === 'error') {
|
||
|
|
console.error('❌ 客户收到错误:', message.message);
|
||
|
|
}
|
||
|
|
} catch (e) {
|
||
|
|
console.error('❌ 解析客户消息失败:', e);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
customerWs.on('error', (error) => {
|
||
|
|
console.error('❌ 客户WebSocket错误:', error);
|
||
|
|
reject(error);
|
||
|
|
});
|
||
|
|
|
||
|
|
customerWs.on('close', () => {
|
||
|
|
console.log('❌ 客户WebSocket连接已关闭');
|
||
|
|
});
|
||
|
|
|
||
|
|
setTimeout(() => {
|
||
|
|
reject(new Error('客户连接或认证超时'));
|
||
|
|
}, 10000);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// 客户发送消息
|
||
|
|
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, // 指定客服ID
|
||
|
|
content: '你好,我是测试客户,有问题咨询',
|
||
|
|
contentType: 1
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
console.log('📤 客户发送消息:', JSON.stringify(message));
|
||
|
|
customerWs.send(JSON.stringify(message));
|
||
|
|
|
||
|
|
// 等待发送确认
|
||
|
|
const messageHandler = (data) => {
|
||
|
|
try {
|
||
|
|
const response = JSON.parse(data.toString());
|
||
|
|
if (response.type === 'message_sent' && response.payload?.messageId === messageId) {
|
||
|
|
customerWs.off('message', messageHandler);
|
||
|
|
console.log('✅ 客户消息发送成功');
|
||
|
|
resolve();
|
||
|
|
}
|
||
|
|
} catch (e) {
|
||
|
|
console.error('❌ 解析客户消息确认失败:', e);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
customerWs.on('message', messageHandler);
|
||
|
|
|
||
|
|
setTimeout(() => {
|
||
|
|
customerWs.off('message', messageHandler);
|
||
|
|
reject(new Error('客户消息发送超时'));
|
||
|
|
}, 10000);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// 客服回复消息
|
||
|
|
function managerReplyMessage() {
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
// 添加随机生成的messageId
|
||
|
|
const messageId = 'test_manager_' + Date.now();
|
||
|
|
const replyMessage = {
|
||
|
|
type: 'chat_message',
|
||
|
|
payload: {
|
||
|
|
messageId: messageId, // 添加messageId
|
||
|
|
conversationId: conversationId, // 使用新获取的会话ID
|
||
|
|
content: '您好,我是客服,请问有什么可以帮助您的?',
|
||
|
|
contentType: 1
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
console.log('📤 客服发送回复消息:', JSON.stringify(replyMessage));
|
||
|
|
managerWs.send(JSON.stringify(replyMessage));
|
||
|
|
messageSent = true;
|
||
|
|
|
||
|
|
// 等待3秒,查看是否有错误回复
|
||
|
|
setTimeout(() => {
|
||
|
|
resolve();
|
||
|
|
}, 3000);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// 清理资源
|
||
|
|
function cleanup() {
|
||
|
|
if (customerWs && customerWs.readyState === WebSocket.OPEN) {
|
||
|
|
customerWs.close();
|
||
|
|
}
|
||
|
|
if (managerWs && managerWs.readyState === WebSocket.OPEN) {
|
||
|
|
managerWs.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 启动调试
|
||
|
|
startDebug();
|