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.
116 lines
3.3 KiB
116 lines
3.3 KiB
// WebSocket调试脚本 - 专注于连接和认证
|
|
const WebSocket = require('ws');
|
|
|
|
// 服务器地址
|
|
const SERVER_URL = 'ws://localhost:3003';
|
|
const customerServiceId = '22'; // 刘杨的ID
|
|
|
|
console.log('=== WebSocket认证详细调试 ===');
|
|
console.log(`连接服务器: ${SERVER_URL}`);
|
|
|
|
// 创建WebSocket连接
|
|
const ws = new WebSocket(SERVER_URL, {
|
|
perMessageDeflate: false,
|
|
headers: {
|
|
'User-Agent': 'Debug-Client',
|
|
'Connection': 'Upgrade'
|
|
}
|
|
});
|
|
|
|
// 连接事件
|
|
ws.on('open', () => {
|
|
console.log('✅ WebSocket连接已建立');
|
|
|
|
// 延迟100ms发送认证消息,确保连接完全就绪
|
|
setTimeout(() => {
|
|
// 使用正确的认证格式 - 必须有type: 'auth'
|
|
const authMessage = {
|
|
type: 'auth', // 必须是'auth'才能被服务器识别为认证消息
|
|
managerId: customerServiceId,
|
|
userType: 'manager' // 用户类型使用不同的字段名
|
|
};
|
|
|
|
const messageString = JSON.stringify(authMessage);
|
|
console.log('📤 发送认证消息:', messageString);
|
|
console.log(' 消息长度:', messageString.length, '字节');
|
|
|
|
try {
|
|
const sent = ws.send(messageString);
|
|
console.log(' 发送结果:', sent ? '成功放入发送队列' : '发送失败');
|
|
} catch (e) {
|
|
console.error(' 发送时异常:', e.message);
|
|
}
|
|
}, 100);
|
|
});
|
|
|
|
// 接收消息事件
|
|
ws.on('message', (data) => {
|
|
console.log('📥 收到服务器消息:');
|
|
try {
|
|
const message = JSON.parse(data.toString());
|
|
console.log(' 消息内容:', JSON.stringify(message, null, 2));
|
|
|
|
if (message.type === 'auth_success') {
|
|
console.log('🎉 认证成功!');
|
|
} else if (message.type === 'auth_error') {
|
|
console.log('❌ 认证失败:', message.message);
|
|
}
|
|
} catch (e) {
|
|
console.error(' 解析消息失败:', e.message);
|
|
console.log(' 原始消息:', data.toString());
|
|
}
|
|
});
|
|
|
|
// 关闭事件
|
|
ws.on('close', (code, reason) => {
|
|
console.log('❌ WebSocket连接已关闭');
|
|
console.log(' 关闭代码:', code);
|
|
console.log(' 关闭原因:', reason.toString());
|
|
|
|
// 常见关闭代码说明
|
|
if (code === 1000) console.log(' 说明: 正常关闭');
|
|
if (code === 1001) console.log(' 说明: 终端离开');
|
|
if (code === 1006) console.log(' 说明: 连接意外关闭');
|
|
if (code === 1011) console.log(' 说明: 服务器内部错误');
|
|
});
|
|
|
|
// 错误事件
|
|
ws.on('error', (error) => {
|
|
console.error('❌ WebSocket错误:');
|
|
console.error(' 错误类型:', error.name);
|
|
console.error(' 错误消息:', error.message);
|
|
console.error(' 错误堆栈:', error.stack);
|
|
});
|
|
|
|
// 发送缓冲区事件
|
|
ws.on('drain', () => {
|
|
console.log('🗑️ 发送缓冲区已清空');
|
|
});
|
|
|
|
// 连接超时处理
|
|
setTimeout(() => {
|
|
if (ws.readyState === WebSocket.OPEN) {
|
|
console.log('⏰ 10秒超时,关闭连接');
|
|
ws.close();
|
|
}
|
|
}, 10000);
|
|
|
|
// 定期检查连接状态
|
|
let checkInterval = setInterval(() => {
|
|
const state = {
|
|
0: 'CONNECTING',
|
|
1: 'OPEN',
|
|
2: 'CLOSING',
|
|
3: 'CLOSED'
|
|
}[ws.readyState];
|
|
|
|
console.log(`🔄 连接状态: ${state}`);
|
|
|
|
if (ws.readyState === WebSocket.CLOSED) {
|
|
clearInterval(checkInterval);
|
|
console.log('\n=== 调试结束 ===');
|
|
}
|
|
}, 1000);
|
|
|
|
console.log('=== 开始调试 ===');
|
|
console.log('按Ctrl+C停止调试');
|
|
|