mirror of https://github.com/waku-org/js-waku.git
Custom nick
This commit is contained in:
parent
cca1d685dc
commit
58838ec898
|
@ -1,4 +1,5 @@
|
||||||
import readline from 'readline';
|
import readline from 'readline';
|
||||||
|
import util from 'util';
|
||||||
|
|
||||||
import Waku from '../lib/waku';
|
import Waku from '../lib/waku';
|
||||||
import { Message } from '../lib/waku_message';
|
import { Message } from '../lib/waku_message';
|
||||||
|
@ -10,6 +11,20 @@ import { ChatMessage } from './chat_message';
|
||||||
(async function () {
|
(async function () {
|
||||||
const opts = processArguments();
|
const opts = processArguments();
|
||||||
|
|
||||||
|
const rl = readline.createInterface({
|
||||||
|
input: process.stdin,
|
||||||
|
output: process.stdout,
|
||||||
|
});
|
||||||
|
|
||||||
|
const question = util.promisify(rl.question).bind(rl);
|
||||||
|
|
||||||
|
// Looks like wrong type definition of promisify is picked.
|
||||||
|
// May be related to https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20497
|
||||||
|
const nick = ((await question(
|
||||||
|
'Please choose a nickname: '
|
||||||
|
)) as unknown) as string;
|
||||||
|
console.log(`Hi ${nick}!`);
|
||||||
|
|
||||||
const waku = await Waku.create({ listenAddresses: [opts.listenAddr] });
|
const waku = await Waku.create({ listenAddresses: [opts.listenAddr] });
|
||||||
|
|
||||||
// TODO: Bubble event to waku, infer topic, decode msg
|
// TODO: Bubble event to waku, infer topic, decode msg
|
||||||
|
@ -50,20 +65,15 @@ import { ChatMessage } from './chat_message';
|
||||||
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
|
waku.libp2p.pubsub.once('gossipsub:heartbeat', resolve)
|
||||||
);
|
);
|
||||||
|
|
||||||
const rl = readline.createInterface({
|
|
||||||
input: process.stdin,
|
|
||||||
output: process.stdout,
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log('Ready to chat!');
|
console.log('Ready to chat!');
|
||||||
rl.prompt();
|
rl.prompt();
|
||||||
rl.on('line', async (line) => {
|
for await (const line of rl) {
|
||||||
rl.prompt();
|
rl.prompt();
|
||||||
const chatMessage = new ChatMessage(new Date(), 'js-chat', line);
|
const chatMessage = new ChatMessage(new Date(), nick, line);
|
||||||
|
|
||||||
const msg = Message.fromBytes(chatMessage.encode());
|
const msg = Message.fromBytes(chatMessage.encode());
|
||||||
await waku.relay.publish(msg);
|
await waku.relay.publish(msg);
|
||||||
});
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
interface Options {
|
interface Options {
|
||||||
|
|
Loading…
Reference in New Issue