Use light push to send messages if it's passed

This commit is contained in:
Franck Royer 2021-05-19 11:59:02 +10:00
parent fb7e9f2b9e
commit 2ea17bfdfa
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 27 additions and 1 deletions

View File

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Implement [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
- Expose `Direction` enum from js-waku root (it was only accessible via the proto module).
- Examples (cli chat): Use light push to send messages if `--lightPush` is passed.
## [0.4.0] - 2021-05-18

View File

@ -6,12 +6,14 @@ import {
Direction,
Environment,
getStatusFleetNodes,
LightPushCodec,
StoreCodec,
Waku,
WakuMessage,
} from 'js-waku';
import TCP from 'libp2p-tcp';
import { multiaddr, Multiaddr } from 'multiaddr';
import PeerId from 'peer-id';
const ChatContentTopic = 'dingpu';
@ -93,6 +95,20 @@ export default async function startChat(): Promise<void> {
}
);
let lightPushNode: PeerId | undefined = undefined;
// Select a node for light pushing (any node).
if (opts.lightPush) {
waku.libp2p.peerStore.on(
'change:protocols',
async ({ peerId, protocols }) => {
if (!lightPushNode && protocols.includes(LightPushCodec)) {
console.log(`Using ${peerId.toB58String()} to light push messages`);
lightPushNode = peerId;
}
}
);
}
console.log('Ready to chat!');
rl.prompt();
for await (const line of rl) {
@ -100,7 +116,11 @@ export default async function startChat(): Promise<void> {
const chatMessage = ChatMessage.fromUtf8String(new Date(), nick, line);
const msg = WakuMessage.fromBytes(chatMessage.encode(), ChatContentTopic);
await waku.relay.send(msg);
if (lightPushNode && opts.lightPush) {
await waku.lightPush.push(lightPushNode, msg);
} else {
await waku.relay.send(msg);
}
}
}
@ -109,6 +129,7 @@ interface Options {
listenAddr: string;
autoDial: boolean;
prod: boolean;
lightPush: boolean;
}
function processArguments(): Options {
@ -119,6 +140,7 @@ function processArguments(): Options {
staticNodes: [],
autoDial: false,
prod: false,
lightPush: false,
};
while (passedArgs.length) {
@ -136,6 +158,9 @@ function processArguments(): Options {
case '--prod':
opts.prod = true;
break;
case '--lightPush':
opts.lightPush = true;
break;
default:
console.log(`Unsupported argument: ${arg}`);
process.exit(1);