-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
45 lines (38 loc) · 909 Bytes
/
Copy pathclient.js
File metadata and controls
45 lines (38 loc) · 909 Bytes
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
/** CLIENT */
'use strict';
const net = require('net');
const PORT = 8080;
const HOST = '127.0.0.1'
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
const socket = new net.Socket();
const exit = () => {
readline.close();
socket.destroy();
process.exit(0);
};
const send = () => readline.question('Type something: ', (input) => {
if (!socket.write(input)) {
socket.pause();
return;
}
console.log(`[${++messagesCount}] Sent: ${input}`);
if (input === 'stop') {
exit();
}
});
let messagesCount = 0;
socket.connect(PORT, HOST);
socket.setEncoding('ascii');
socket.on('data', (data) => {
if (data === 'stop') {
exit();
}
console.log(`[${++messagesCount}] Received: ${data}`);
send();
});
socket.on('drain', () => socket.resume());
socket.on('end', () => exit());
send();