-
Notifications
You must be signed in to change notification settings - Fork 17
/
client.js
54 lines (42 loc) · 1.58 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
'use strict';
const nkn = require('../lib');
const useMultiClient = true;
(async function () {
let Client = useMultiClient ? nkn.MultiClient : nkn.Client;
let alice = new Client({ identifier: 'alice' });
let bob = new Client({ identifier: 'bob' });
console.log('Secret seed:', alice.getSeed());
alice.onConnectFailed(() => {
console.error('Alice connect failed');
});
if (useMultiClient) {
for (let clientID of Object.keys(alice.clients)) {
alice.clients[clientID].onConnectFailed(() => {
console.error('Alice client', clientID, 'connect failed');
});
}
}
await Promise.all([
new Promise((resolve, reject) => alice.onConnect(resolve)),
new Promise((resolve, reject) => bob.onConnect(resolve)),
]);
await new Promise((resolve, reject) => setTimeout(resolve, 1000));
let timeSent = Date.now();
bob.onMessage(async ({ src, payload, isEncrypted }) => {
console.log('Receive', isEncrypted ? 'encrypted' : 'unencrypted', 'message', '"' + payload + '"','from', src, 'after', Date.now() - timeSent, 'ms');
// For byte array response:
// return Uint8Array.from([1,2,3,4,5])
return 'Well received!';
});
try {
console.log('Send message from', alice.addr, 'to', bob.addr);
// For byte array data:
// let reply = await alice.send(bob.addr, Uint8Array.from([1,2,3,4,5]));
let reply = await alice.send(bob.addr, 'Hello world!');
console.log('Receive reply', '"' + reply + '"', 'from', bob.addr, 'after', Date.now() - timeSent, 'ms');
} catch (e) {
console.error(e);
}
alice.close();
bob.close();
})();