-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
35 lines (28 loc) · 850 Bytes
/
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
/**
* client is anyone that can connect to the server.js
**/
const net = require("net");
const readline = require("readline/promises");
//
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// once this connection is made, this callback will run
// createConnection() will return a socket object with available methods
// this socket is what allows us to make a connection to the server!
const socket = net.createConnection(
{ host: "127.0.0.1", port: 3000 },
async () => {
console.log("connected to the server!");
const message = await rl.question("Enter a message > ");
socket.write(message);
}
);
// receiving chat data back from server
socket.on("data", (data) => {
console.log(data.toString("utf-8"));
});
socket.on("end", () => {
console.log("Connection was ended!");
});