-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathwebsocket-test.js
More file actions
100 lines (87 loc) Β· 3.09 KB
/
websocket-test.js
File metadata and controls
100 lines (87 loc) Β· 3.09 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
96
97
98
99
100
/**
* @fileoverview Simple WebSocket connection test with periodic stats polling
* @description Lightweight test utility that connects to the synchronizer WebSocket
* and sends stats requests every 10 seconds. Useful for continuous monitoring.
*
* @module websocket-test
* @requires ws
*
* Usage: node websocket-test.js
*
* Features:
* - Connects to ws://localhost:3333
* - Sends stats request immediately on connection
* - Polls for stats every 10 seconds
* - Displays key metrics: sessions, users, points, traffic, connection state
* - Graceful shutdown on Ctrl+C
*/
const WebSocket = require('ws');
console.log('π§ͺ Testing WebSocket connection to ws://localhost:3333...');
console.log('π Will send stats requests every 10 seconds - press Ctrl+C to stop');
const ws = new WebSocket('ws://localhost:3333');
let requestCount = 0;
let statsInterval;
ws.on('open', () => {
console.log('β
WebSocket connected successfully!');
// Send initial stats request
sendStatsRequest();
// Set up periodic stats requests every 10 seconds
statsInterval = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
sendStatsRequest();
} else {
console.log('β WebSocket not open, stopping requests');
clearInterval(statsInterval);
}
}, 10000);
});
/**
* Sends a stats request to the WebSocket server
* @function sendStatsRequest
* @complexity O(1) - Simple message send
* @calls ws.send, JSON.stringify
* @calledBy open event handler, periodic interval
* @description Increments request counter and sends { what: 'stats' } message
* with timestamp logging for request tracking
*/
function sendStatsRequest() {
requestCount++;
const statsRequest = { what: 'stats' };
ws.send(JSON.stringify(statsRequest));
console.log(`π‘ [${requestCount}] Sent stats request at ${new Date().toLocaleTimeString()}`);
}
ws.on('message', (data) => {
try {
const message = JSON.parse(data.toString());
const timestamp = new Date().toLocaleTimeString();
console.log(`π¨ [${timestamp}] Received message type: ${message.what || 'unknown'}`);
if (message.what === 'stats' && message.value) {
const stats = message.value;
console.log(`β
Stats data - Sessions: ${stats.sessions}, Users: ${stats.users}, Points: ${stats.walletLifePoints}, Traffic: ${stats.syncLifeTraffic}, Connection: ${stats.proxyConnectionState}`);
} else {
console.log(`π Raw message: ${data.toString().substring(0, 150)}...`);
}
} catch (error) {
console.log(`π Raw data: ${data.toString().substring(0, 150)}...`);
}
});
ws.on('error', (error) => {
console.log('β WebSocket error:', error.message);
clearInterval(statsInterval);
process.exit(1);
});
ws.on('close', (code, reason) => {
console.log(`π WebSocket closed: ${code} ${reason.toString()}`);
clearInterval(statsInterval);
process.exit(0);
});
// Handle Ctrl+C gracefully
process.on('SIGINT', () => {
console.log('\nπ Stopping WebSocket test...');
clearInterval(statsInterval);
if (ws.readyState === WebSocket.OPEN) {
ws.close();
} else {
process.exit(0);
}
});