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.
 
 

96 lines
2.4 KiB

// 测试聊天功能连接的脚本
const WebSocket = require('ws');
// 假设服务器WebSocket地址
const SERVER_URL = 'ws://localhost:3000'; // 根据实际服务器地址调整
// 模拟用户和客服的连接
function testUserToManagerCommunication() {
console.log('开始测试用户和客服之间的消息传递...');
// 模拟客服连接
const managerSocket = new WebSocket(SERVER_URL);
managerSocket.on('open', () => {
console.log('客服连接已建立');
// 客服认证
managerSocket.send(JSON.stringify({
type: 'auth',
data: {
userId: 'manager_1',
type: 'manager',
name: '测试客服'
}
}));
});
managerSocket.on('message', (data) => {
try {
const message = JSON.parse(data.toString());
console.log('客服收到消息:', message);
} catch (e) {
console.error('客服解析消息失败:', e);
}
});
managerSocket.on('error', (error) => {
console.error('客服连接错误:', error);
});
// 延迟2秒后创建用户连接
setTimeout(() => {
const userSocket = new WebSocket(SERVER_URL);
userSocket.on('open', () => {
console.log('用户连接已建立');
// 用户认证
userSocket.send(JSON.stringify({
type: 'auth',
data: {
userId: 'user_1',
type: 'user',
name: '测试用户'
}
}));
// 再延迟1秒后发送消息
setTimeout(() => {
console.log('用户发送测试消息...');
userSocket.send(JSON.stringify({
type: 'chat_message',
data: {
managerId: 'manager_1',
content: '这是一条测试消息',
contentType: 1, // 文本消息
timestamp: Date.now()
}
}));
}, 1000);
});
userSocket.on('message', (data) => {
try {
const message = JSON.parse(data.toString());
console.log('用户收到消息:', message);
} catch (e) {
console.error('用户解析消息失败:', e);
}
});
userSocket.on('error', (error) => {
console.error('用户连接错误:', error);
});
// 清理连接
setTimeout(() => {
console.log('测试完成,关闭连接');
userSocket.close();
managerSocket.close();
}, 10000);
}, 2000);
}
// 运行测试
testUserToManagerCommunication();