131 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-03-24 17:08:15 +11:00
import readline from 'readline';
2021-04-01 10:37:16 +11:00
import util from 'util';
2021-03-24 17:08:15 +11:00
import { ChatMessage, StoreCodec, Waku, WakuMessage } from 'js-waku';
import TCP from 'libp2p-tcp';
2021-04-20 16:51:04 +10:00
import { multiaddr, Multiaddr } from 'multiaddr';
2021-03-30 09:24:29 +11:00
2021-04-07 11:04:30 +10:00
const ChatContentTopic = 'dingpu';
2021-05-04 15:12:36 +10:00
export default async function startChat(): Promise<void> {
const opts = processArguments();
2021-03-24 17:08:15 +11:00
const waku = await Waku.create({
listenAddresses: [opts.listenAddr],
modules: { transport: [TCP] },
});
console.log('PeerId: ', waku.libp2p.peerId.toB58String());
console.log('Listening on ');
waku.libp2p.multiaddrs.forEach((address) => {
console.log(`\t- ${address}`);
});
2021-04-01 10:37:16 +11:00
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let nick = 'js-waku';
2021-04-20 10:07:14 +10:00
try {
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
nick = ((await question(
'Please choose a nickname: '
)) as unknown) as string;
} catch (e) {
2021-04-21 09:57:21 +10:00
console.log('Using default nick. Due to ', e);
2021-04-20 10:07:14 +10:00
}
2021-04-01 10:37:16 +11:00
console.log(`Hi, ${nick}!`);
2021-03-26 12:10:26 +11:00
waku.relay.addObserver(
(message) => {
if (message.payload) {
const chatMsg = ChatMessage.decode(message.payload);
console.log(formatMessage(chatMsg));
}
},
[ChatContentTopic]
);
2021-03-26 12:10:26 +11:00
if (opts.staticNode) {
console.log(`Dialing ${opts.staticNode}`);
await waku.dial(opts.staticNode);
2021-04-07 11:04:30 +10:00
}
// If we connect to a peer with WakuStore, we run the protocol
// TODO: Instead of doing it `once` it should always be done but
// only new messages should be printed
waku.libp2p.peerStore.once(
'change:protocols',
async ({ peerId, protocols }) => {
if (protocols.includes(StoreCodec)) {
console.log(
`Retrieving archived messages from ${peerId.toB58String()}`
);
const messages = await waku.store.queryHistory(peerId, [
ChatContentTopic,
]);
messages?.map((msg) => {
if (msg.payload) {
const chatMsg = ChatMessage.decode(msg.payload);
2021-05-04 15:12:36 +10:00
console.log(formatMessage(chatMsg));
}
});
}
}
);
2021-03-24 17:08:15 +11:00
console.log('Ready to chat!');
rl.prompt();
2021-04-01 10:37:16 +11:00
for await (const line of rl) {
rl.prompt();
const chatMessage = ChatMessage.fromUtf8String(new Date(), nick, line);
2021-04-07 11:04:30 +10:00
const msg = WakuMessage.fromBytes(chatMessage.encode(), ChatContentTopic);
await waku.relay.send(msg);
2021-04-01 10:37:16 +11:00
}
2021-05-04 15:12:36 +10:00
}
interface Options {
2021-04-07 11:04:30 +10:00
staticNode?: Multiaddr;
listenAddr: string;
}
function processArguments(): Options {
2021-03-30 09:24:29 +11:00
const passedArgs = process.argv.slice(2);
2021-03-30 09:24:29 +11:00
let opts: Options = { listenAddr: '/ip4/0.0.0.0/tcp/0' };
while (passedArgs.length) {
const arg = passedArgs.shift();
switch (arg) {
case '--staticNode':
2021-04-07 11:04:30 +10:00
opts = Object.assign(opts, {
2021-04-20 16:51:04 +10:00
staticNode: multiaddr(passedArgs.shift()!),
2021-04-07 11:04:30 +10:00
});
break;
case '--listenAddr':
opts = Object.assign(opts, { listenAddr: passedArgs.shift() });
break;
default:
console.log(`Unsupported argument: ${arg}`);
2021-03-30 09:24:29 +11:00
process.exit(1);
}
}
return opts;
}
2021-04-07 11:04:30 +10:00
2021-05-04 15:12:36 +10:00
export function formatMessage(chatMsg: ChatMessage): string {
2021-04-07 11:04:30 +10:00
const timestamp = chatMsg.timestamp.toLocaleString([], {
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: '2-digit',
hour12: false,
});
return `<${timestamp}> ${chatMsg.nick}: ${chatMsg.payloadAsUtf8}`;
2021-04-07 11:04:30 +10:00
}