Auto select peer if none provided for store and light push protocols

This commit is contained in:
Franck Royer
2021-06-16 23:51:20 +10:00
parent 60eb473047
commit 939b5fb20a
7 changed files with 56 additions and 36 deletions
+2 -18
View File
@@ -6,7 +6,6 @@ import {
Direction,
Environment,
getStatusFleetNodes,
LightPushCodec,
Protocol,
StoreCodec,
Waku,
@@ -14,7 +13,6 @@ import {
} from 'js-waku';
import TCP from 'libp2p-tcp';
import { multiaddr, Multiaddr } from 'multiaddr';
import PeerId from 'peer-id';
const ChatContentTopic = '/toy-chat/2/huilong/proto';
@@ -100,20 +98,6 @@ 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) {
@@ -121,8 +105,8 @@ export default async function startChat(): Promise<void> {
const chatMessage = ChatMessage.fromUtf8String(new Date(), nick, line);
const msg = WakuMessage.fromBytes(chatMessage.encode(), ChatContentTopic);
if (lightPushNode && opts.lightPush) {
await waku.lightPush.push(lightPushNode, msg);
if (opts.lightPush) {
await waku.lightPush.push(msg);
} else {
await waku.relay.send(msg);
}