-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest-ws.js
More file actions
36 lines (29 loc) · 959 Bytes
/
test-ws.js
File metadata and controls
36 lines (29 loc) · 959 Bytes
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
import { WebSocket } from 'ws';
console.log('Script started');
// Try 127.0.0.1 to avoid localhost resolution ambiguity
const url = 'ws://127.0.0.1:4000/ws/state';
console.log(`Attempting connection to ${url}...`);
try {
const ws = new WebSocket(url);
ws.on('headers', (headers, response) => {
console.log('Received headers:', headers);
console.log('Response status:', response.statusCode, response.statusMessage);
});
ws.on('open', () => {
console.log('Connected successfully!');
ws.close();
});
ws.on('error', (err) => {
console.error('Connection Error:', err);
});
ws.on('close', (code, reason) => {
console.log(`Connection Closed: Code=${code}, Reason=${reason.toString()}`);
});
} catch (e) {
console.error('Synchronous error creating WebSocket:', e);
}
// Timeout after 5 seconds to prevent hanging
setTimeout(() => {
console.log('Timeout reached (5s), exiting...');
process.exit(1);
}, 5000);