Seem to fix receiving messages

Looking at runs where receiving messages work vs doesn't, in the case it
 does not work, js-chat is still receiving some messages as part
 of the identify process after subscribing.
 Whereas, when it works, the subscribe is done after those messages.
 With a 2s sleep to delay the subscribe, it seems that it always works.
This commit is contained in:
Franck Royer 2021-03-29 17:00:24 +11:00
parent b4e5d6d93c
commit d9decabae9
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 11 additions and 4 deletions

View File

@ -8,7 +8,7 @@ import { delay } from '../build/main/test_utils/delay';
;(async function() { ;(async function() {
const opts = processArguments(); const opts = processArguments();
const waku = await Waku.create({ listenAddresses: ['/ip4/0.0.0.0/tcp/55123'] }); const waku = await Waku.create({ listenAddresses: [opts.listenAddr] });
// TODO: Bubble event to waku, infere topic, decode msg // TODO: Bubble event to waku, infere topic, decode msg
waku.libp2p.pubsub.on(TOPIC, event => { waku.libp2p.pubsub.on(TOPIC, event => {
@ -21,13 +21,15 @@ import { delay } from '../build/main/test_utils/delay';
if (opts.staticNode) { if (opts.staticNode) {
console.log(`dialing ${opts.staticNode}`); console.log(`dialing ${opts.staticNode}`);
await waku.dial(opts.staticNode); await waku.dial(opts.staticNode);
await delay(100);
} }
await new Promise((resolve) => await new Promise((resolve) =>
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve) waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
); );
// TODO: identify if it is possible to listen to an event to confirm dial
// finished instead of an arbitrary delay.
await delay(2000);
// TODO: Automatically subscribe // TODO: Automatically subscribe
await waku.relay.subscribe(); await waku.relay.subscribe();
console.log('Subscribed to waku relay'); console.log('Subscribed to waku relay');
@ -53,12 +55,13 @@ import { delay } from '../build/main/test_utils/delay';
interface Options { interface Options {
staticNode?: string; staticNode?: string;
listenAddr: string;
} }
function processArguments(): Options { function processArguments(): Options {
let passedArgs = process.argv.slice(2); let passedArgs = process.argv.slice(2);
let opts: Options = {}; let opts: Options = {listenAddr: '/ip4/0.0.0.0/tcp/0'};
while (passedArgs.length) { while (passedArgs.length) {
const arg = passedArgs.shift(); const arg = passedArgs.shift();
@ -66,8 +69,12 @@ function processArguments(): Options {
case '--staticNode': case '--staticNode':
opts = Object.assign(opts, { staticNode: passedArgs.shift() }); opts = Object.assign(opts, { staticNode: passedArgs.shift() });
break; break;
case '--listenAddr':
opts = Object.assign(opts, { listenAddr: passedArgs.shift() });
break;
default: default:
console.log(`Argument ignored: ${arg}`); console.log(`Unsupported argument: ${arg}`);
process.exit(1)
} }
} }