From b4e5d6d93c218ab106aabe8167cc5d2a97eee4eb Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Mon, 29 Mar 2021 16:46:32 +1100 Subject: [PATCH] Get peer connection details from command line --- chat/index.ts | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/chat/index.ts b/chat/index.ts index a584d33e6c..4b074b77c4 100644 --- a/chat/index.ts +++ b/chat/index.ts @@ -6,8 +6,9 @@ import readline from 'readline'; 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: ['/ip4/0.0.0.0/tcp/55123'] }); // TODO: Bubble event to waku, infere topic, decode msg waku.libp2p.pubsub.on(TOPIC, event => { @@ -16,17 +17,12 @@ import { delay } from '../build/main/test_utils/delay'; }); console.log('Waku started'); - // Status static node - await waku.dial('/ip4/134.209.139.210/tcp/30303/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ'); - // Richard's node - // await waku.dial('/ip4/134.209.113.86/tcp/9000/p2p/16Uiu2HAmVVi6Q4j7MAKVibquW8aA27UNrA4Q8Wkz9EetGViu8ZF1'); - - // await waku.dial('/ip4/0.0.0.0/tcp/60000/p2p/16Uiu2HAmDVYacyxN4t1SYBhRSTDr6nmYwuY6qWWTgagZm558rFA6') - - await delay(100); - - console.log('Static node has been dialed'); + 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) @@ -48,9 +44,32 @@ import { delay } from '../build/main/test_utils/delay'; console.log('Ready to chat!'); rl.prompt(); rl.on('line', async (line) => { - rl.prompt(); - const msg = Message.fromUtf8String('(js-chat) ' + line); - await waku.relay.publish(msg); + rl.prompt(); + const msg = Message.fromUtf8String('(js-chat) ' + line); + await waku.relay.publish(msg); }); })(); + +interface Options { + staticNode?: string; +} + +function processArguments(): Options { + let passedArgs = process.argv.slice(2); + + let opts: Options = {}; + + while (passedArgs.length) { + const arg = passedArgs.shift(); + switch (arg) { + case '--staticNode': + opts = Object.assign(opts, { staticNode: passedArgs.shift() }); + break; + default: + console.log(`Argument ignored: ${arg}`); + } + } + + return opts; +}