mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-04 06:43:12 +00:00
Use light push to send messages if it's passed
This commit is contained in:
parent
fb7e9f2b9e
commit
2ea17bfdfa
@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
### Added
|
### Added
|
||||||
- Implement [Waku v2 Light Push protocol](https://rfc.vac.dev/spec/19/).
|
- 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).
|
- 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
|
## [0.4.0] - 2021-05-18
|
||||||
|
|
||||||
|
|||||||
@ -6,12 +6,14 @@ import {
|
|||||||
Direction,
|
Direction,
|
||||||
Environment,
|
Environment,
|
||||||
getStatusFleetNodes,
|
getStatusFleetNodes,
|
||||||
|
LightPushCodec,
|
||||||
StoreCodec,
|
StoreCodec,
|
||||||
Waku,
|
Waku,
|
||||||
WakuMessage,
|
WakuMessage,
|
||||||
} from 'js-waku';
|
} from 'js-waku';
|
||||||
import TCP from 'libp2p-tcp';
|
import TCP from 'libp2p-tcp';
|
||||||
import { multiaddr, Multiaddr } from 'multiaddr';
|
import { multiaddr, Multiaddr } from 'multiaddr';
|
||||||
|
import PeerId from 'peer-id';
|
||||||
|
|
||||||
const ChatContentTopic = 'dingpu';
|
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!');
|
console.log('Ready to chat!');
|
||||||
rl.prompt();
|
rl.prompt();
|
||||||
for await (const line of rl) {
|
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 chatMessage = ChatMessage.fromUtf8String(new Date(), nick, line);
|
||||||
|
|
||||||
const msg = WakuMessage.fromBytes(chatMessage.encode(), ChatContentTopic);
|
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;
|
listenAddr: string;
|
||||||
autoDial: boolean;
|
autoDial: boolean;
|
||||||
prod: boolean;
|
prod: boolean;
|
||||||
|
lightPush: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
function processArguments(): Options {
|
function processArguments(): Options {
|
||||||
@ -119,6 +140,7 @@ function processArguments(): Options {
|
|||||||
staticNodes: [],
|
staticNodes: [],
|
||||||
autoDial: false,
|
autoDial: false,
|
||||||
prod: false,
|
prod: false,
|
||||||
|
lightPush: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
while (passedArgs.length) {
|
while (passedArgs.length) {
|
||||||
@ -136,6 +158,9 @@ function processArguments(): Options {
|
|||||||
case '--prod':
|
case '--prod':
|
||||||
opts.prod = true;
|
opts.prod = true;
|
||||||
break;
|
break;
|
||||||
|
case '--lightPush':
|
||||||
|
opts.lightPush = true;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
console.log(`Unsupported argument: ${arg}`);
|
console.log(`Unsupported argument: ${arg}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user