-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
46 lines (37 loc) · 963 Bytes
/
Copy pathclient.js
File metadata and controls
46 lines (37 loc) · 963 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
46
const colors = require("colors");
const net = require('net');
let readlinesync = require("readline-sync");
const client = net.createConnection({ port: 8124 }, () => {
console.log('connected to server!');
});
client.on('data', (data) => {
console.log('received => ' + colors.blue(data.toString()));
});
client.on('end', () => {
console.log('disconnected from server');
});
client.on('error', () => {
console.log('Error in connecting to server');
});
function sendData(data) {
client.write(data, "UTF8", () => {
console.log('message has been sent');
});
}
function closeConnection() {
if(client !== null) {
client.end();
}
}
function getUserInput() {
let data = readlinesync.question(colors.blue("\nEnter data to send: "));
sendData(data);
// Defer the execution so that node can process incoming message
setTimeout(() => {
getUserInput();
}, 10);
}
// Defer the execution
setTimeout(() => {
getUserInput();
}, 10);