-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo-client.js
63 lines (51 loc) · 1.82 KB
/
demo-client.js
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
window.addEventListener('load', () => {
const { Client, BinaryEncoder, Codec, Int8Codec, Int16Codec, LongIntCodec, BooleanCodec, StringCodec } = netcode;
// Register your events
const encoder = new BinaryEncoder([
['id', new Int16Codec()],
['ping', new LongIntCodec(6)],
['pong', new LongIntCodec(6)],
['inverse', new BooleanCodec()],
['greeting', new StringCodec()],
['total', new Int8Codec()],
]);
// Create the client
const client = new Client('ws://127.0.0.1:8002', encoder);
let ping;
// Listen for a "pong" event
client.on('pong', ({ detail: pong }) => {
console.info('pong: %s ms', pong - ping);
});
// Listen for an "id" event
client.on('id', ({ detail: id }) => {
console.log('connected with id %s', id);
ping = Date.now();
// Send a "ping" event
client.send('ping', ping);
});
// Listen for a "total" event
client.on('total', ({ detail: total }) => {
console.log(`There is ${total} people connected.`)
});
// Listen for an "inverse" event
client.on('inverse', ({ detail: status }) => {
// Answer with an "inverse" event
client.send('inverse', !status);
console.log('Inverse received: %s', status);
// Send a "greeting" event
client.send('greeting', 'Hello, I\'m client 😊!');
});
// Listen for a "greeting" event
client.on('greeting', ({ detail: message }) => {
console.log('Servers geets you: "%s"', message);
});
// Listen for oppening connection
client.on('open', () => {
console.info('Connection open.');
setTimeout(() => client.close(), 20 * 1000);
});
// Listen for connection close
client.on('close', () => {
console.info('Connection closed.');
});
});