From d9decabae903b5050dd5cf733a29930c1e58cbc1 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Mon, 29 Mar 2021 17:00:24 +1100 Subject: [PATCH] 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. --- chat/index.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/chat/index.ts b/chat/index.ts index 4b074b77c4..3b21e418bf 100644 --- a/chat/index.ts +++ b/chat/index.ts @@ -8,7 +8,7 @@ import { delay } from '../build/main/test_utils/delay'; ;(async function() { 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 waku.libp2p.pubsub.on(TOPIC, event => { @@ -21,13 +21,15 @@ import { delay } from '../build/main/test_utils/delay'; if (opts.staticNode) { console.log(`dialing ${opts.staticNode}`); await waku.dial(opts.staticNode); - await delay(100); } await new Promise((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 await waku.relay.subscribe(); console.log('Subscribed to waku relay'); @@ -53,12 +55,13 @@ import { delay } from '../build/main/test_utils/delay'; interface Options { staticNode?: string; + listenAddr: string; } function processArguments(): Options { let passedArgs = process.argv.slice(2); - let opts: Options = {}; + let opts: Options = {listenAddr: '/ip4/0.0.0.0/tcp/0'}; while (passedArgs.length) { const arg = passedArgs.shift(); @@ -66,8 +69,12 @@ function processArguments(): Options { case '--staticNode': opts = Object.assign(opts, { staticNode: passedArgs.shift() }); break; + case '--listenAddr': + opts = Object.assign(opts, { listenAddr: passedArgs.shift() }); + break; default: - console.log(`Argument ignored: ${arg}`); + console.log(`Unsupported argument: ${arg}`); + process.exit(1) } }