-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
55 lines (44 loc) · 1.29 KB
/
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
import { setTimeout as sleep } from 'timers/promises';
import { Client } from '@sofidev/ipc';
import { Timer } from './Timer.js';
const name = process.argv[2];
const Job = /**@type {const}*/ ({
Ping: 0,
Pong: 1,
GenerateImage: 2,
Unknown: 3,
});
const client = new Client(name, {
maximumRetries: Infinity,
retryTime: 2_000,
});
client.on('ready', () => {
console.log(`${name}: Ready`);
});
client.on('connecting', () => {
console.log(`${name}: Connecting`);
});
client.on('message', (message) => {
console.log(`Received message at ${name}:`, message.data);
});
// client.on('raw', (message) => {
// console.log(thing);
// console.log(`Received raw message at ${name}:`, message);
// timer.timeEnd(thing, true, (t) => `Time taken: ${t}`);
// });
client.on('disconnect', () => {
console.log('Connection lost');
});
await client.connectTo(3000, '127.0.0.1');
console.log(`${name}: Connected to Rust server`);
await sleep(5000);
const timer = new Timer();
for (let i = 0; i < 100; i++) {
// Send a message to the Rust server
const message = { payload: `ping`, job: Job.Ping };
let thing = timer.time();
const msg = await client.sendTo('Sofi', message, { receptive: true });
timer.timeEnd(thing, true, (t) => `Time taken: ${t}`);
console.log(msg);
// await sleep(1000);
}