-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (31 loc) · 851 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import chalk from 'chalk';
import readline from 'readline';
import Chat from './chat.js';
import Discovery from './discovery.js';
import { getIPv4Address, generateRandomId } from './utils.js';
const address = getIPv4Address();
const id = generateRandomId();
console.log(chalk.blue.bold('local-p2p-chat'));
console.log(`${chalk.blue('IP4-Address')}: ${address}`);
console.log(`${chalk.blue('Peer ID')}: ${id}`);
console.log();
const chat = new Chat({ id });
chat.emitter.on('ready', (port) => {
const discovery = new Discovery({
topic: 'p2p-chat',
address,
port,
id,
});
discovery.emitter.on('peer', (peer) => {
chat.addPeer(peer);
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
rl.on('line', (line) => {
chat.send(line);
});
});