-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-server-websocket.js
More file actions
95 lines (78 loc) · 2.89 KB
/
test-server-websocket.js
File metadata and controls
95 lines (78 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* 测试服务器WebSocket支持
*/
const WebSocket = require('ws');
// 服务器配置
const BASE_URL = 'http://123.56.246.44:80';
const WS_URL = BASE_URL.replace('http://', 'ws://').replace('https://', 'wss://') + '/ws/conversations/send_audio_message';
console.log('🚀 开始测试服务器WebSocket支持...');
console.log('🔗 基础URL:', BASE_URL);
console.log('🔗 WebSocket URL:', WS_URL);
// 测试不同的连接方式
const testCases = [
{
name: '基本连接测试',
url: WS_URL + '?token=test_token&conversation_id=test_123',
description: '使用简单的测试token'
},
{
name: '完整token测试',
url: WS_URL + '?token=Bearer%20eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxLCJ1c2VybmFtZSI6Im1lbG9uX3VzZXIiLCJhdXRoX3R5cGUiOiJlbWFpbCIsImmlkZW50aWZpZXIiOiIyOTgyODc1NDkyQHFxLmNvbSIsImV4cCI6MTc1MzA2MjcyNX0.bUZYCp4w_zx0OLYQdHMwoXQytI-Afkptp2y9vOZk7wU&conversation_id=test_123',
description: '使用完整的JWT token'
}
];
async function testWebSocketConnection(testCase) {
return new Promise((resolve) => {
console.log(`\n🧪 ${testCase.name}:`);
console.log(`📝 ${testCase.description}`);
console.log(`🔗 URL: ${testCase.url}`);
const ws = new WebSocket(testCase.url);
const timeout = setTimeout(() => {
console.log('⏰ 连接超时');
ws.close();
resolve({ success: false, error: 'timeout' });
}, 10000);
ws.on('open', () => {
clearTimeout(timeout);
console.log('✅ 连接成功!');
// 发送简单的测试消息
const testMessage = { type: 'ping', timestamp: Date.now() };
ws.send(JSON.stringify(testMessage));
console.log('📤 发送测试消息:', JSON.stringify(testMessage));
// 等待响应
setTimeout(() => {
ws.close();
resolve({ success: true });
}, 2000);
});
ws.on('message', (data) => {
console.log('📥 收到响应:', data.toString());
});
ws.on('error', (error) => {
clearTimeout(timeout);
console.log('❌ 连接错误:', error.message);
resolve({ success: false, error: error.message });
});
ws.on('close', (code, reason) => {
clearTimeout(timeout);
console.log('🔌 连接关闭 - 代码:', code, '原因:', reason);
resolve({ success: false, error: `closed with code ${code}` });
});
});
}
async function runTests() {
console.log('=' * 50);
console.log('🧪 WebSocket服务器测试');
console.log('=' * 50);
for (const testCase of testCases) {
const result = await testWebSocketConnection(testCase);
console.log(`\n📊 测试结果: ${result.success ? '✅ 成功' : '❌ 失败'}`);
if (!result.success) {
console.log(`❌ 错误: ${result.error}`);
}
console.log('-'.repeat(50));
}
console.log('\n🏁 测试完成');
}
// 运行测试
runTests().catch(console.error);