-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
45 lines (36 loc) · 1.02 KB
/
client.js
File metadata and controls
45 lines (36 loc) · 1.02 KB
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
import net from "node:net";
import chalk from "chalk";
import readline from "node:readline";
const [, , host, portArg] = process.argv;
if (!host || !portArg) {
console.log("Usage: node client.js <host> <port>");
process.exit(1);
}
const port = Number(portArg);
const socket = net.createConnection({ host, port });
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: chalk.green('user@user:~$') + " "
});
socket.on("connect", () => {
console.log(`Connected to ${host}:${port}`);
console.log("Run 'help' command for help")
console.log(chalk.greenBright("-----------------------------------------"))
rl.prompt()
});
rl.on("line", line => {
socket.write(line + "\n");
});
socket.on("data", data => {
const response = data.toString().trim()
console.log(chalk.bold(response))
rl.prompt()
})
socket.on("error", (err) => {
console.error("connection error:", err.message);
process.exit(1);
});
socket.on("close", () => {
process.exit();
});