-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
60 lines (48 loc) · 1.23 KB
/
client.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const readline = require('readline');
const WebSocket = require('ws');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
async function startClient(url) {
const name = await new Promise((resolve) => {
rl.question('Please enter your name: ', (answer) => {
resolve(answer);
});
});
const ws = new WebSocket(url);
ws.on('open', () => {
console.log(`Connected to ${url}`);
// Start listening for user input
rl.on('line', (input) => {
if (input.startsWith('/')) {
ws.send(input);
} else {
ws.send(`${name}: ${input}`);
}
});
});
ws.on('message', (message) => {
console.log('Received:', message);
});
ws.on('close', () => {
console.log('Connection closed');
process.exit(0);
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
});
}
const args = {
host: '127.0.0.1',
port: 5500,
};
const url = `ws://${args.host}:${args.port}/ws`;
console.log(`
/list list all available rooms
/join name join room, if room does not exist, create new one
/name name set session name
some message just string, send message to all peers in same room
ctrl-D to exit
`);
startClient(url);