-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ping.js
More file actions
43 lines (43 loc) · 1.39 KB
/
test_ping.js
File metadata and controls
43 lines (43 loc) · 1.39 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
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ws_1 = __importDefault(require("ws"));
const protocol_1 = require("./src/protocol");
const ws = new ws_1.default('ws://localhost:8080');
let pingCount = 0;
ws.on('open', () => {
console.log('Connected to server');
// Send initial handshake if needed, or just wait for ping
// Protocol says connectionId is needed in header? Connection class handles null.
});
ws.on('message', (data) => {
const msg = JSON.parse(data.toString());
if (msg.header.type === protocol_1.MessageType.PING) {
console.log('Received PING');
pingCount++;
if (pingCount <= 1) {
console.log('Sending PONG...');
ws.send(JSON.stringify({
header: {
streamId: '',
connectionId: 'test-client',
type: protocol_1.MessageType.PONG,
timestamp: Date.now()
},
payload: null
}));
}
else {
console.log('Ignoring PING to trigger timeout...');
}
}
});
ws.on('close', () => {
console.log('Disconnected');
process.exit(0);
});
ws.on('error', (err) => {
console.error('Error:', err);
});