-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.ts
61 lines (53 loc) · 1.67 KB
/
client.ts
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
61
// LOADING environmental variables
import * as dotenv from 'dotenv';
dotenv.config();
// IMPORTING 'socket.io-client' module
import { io } from 'socket.io-client';
// IMPORTING readline module to read from console.
import * as readline from 'readline';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question(`Enter server address [${process.env.ADDRESS || 'localhost'}]: `, (address) => {
rl.question(`Enter server port [${process.env.PORT || '3000'}]: `, (port) => {
if (address === '') address = process.env.ADDRESS || 'localhost';
if (port === '') port = process.env.PORT || '3000';
// Creating client at the server
console.log('Connecting to server...');
const socket = io(`http://${address}:${port}`, { forceNew: true });
socket.on('connect', () => {
console.log('Connected.');
function newUser() {
rl.question('Enter your name: ', (username) => {
socket.emit('new user', username, (res: Array<unknown>) => {
console.log(res[1]);
if (res[0] === 200) {
process.stdout.write('\n> ');
rl.prompt();
} else if (res[0] === 400) {
newUser();
} else {
console.log('Something went wrong. Check for updates on the GitHub repo:');
console.log('https://github.com/Dennis1507/chat/releases');
}
});
});
}
newUser();
});
socket.on('message', (text) => {
// Erasing last line
process.stdout.clearLine(0);
process.stdout.cursorTo(0);
console.log(text);
process.stdout.write('> ');
rl.prompt(true);
});
rl.on('line', (text) => {
socket.emit('message', text.trim());
process.stdout.write('> ');
rl.prompt();
});
});
});