-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
78 lines (66 loc) · 2.19 KB
/
server.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
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* WebSocket server that allows establishing a TCP connection with a
* Remote Server and send messages over TCP connection.
*
* Works as an Adapter between the WebSocket client and the TCP server.
*/
const WebSocket = require("./node_modules/ws");
const net = require("net");
const dotenv = require("dotenv");
// Load environment variables from .env file
dotenv.config();
const WEBSOCKET_PORT = process.env.WEBSOCKET_PORT || 8080;
// Create a WebSocket server
const wss = new WebSocket.Server({ port: WEBSOCKET_PORT });
wss.on("connection", (ws) => {
let tcpClient = null;
ws.on("message", (message) => {
const [command, ip, port, ...msg] = message.toString().split(" ");
console.info(`Received message from Client: ${message}`);
if (command === "connect") {
// if there's an active connection, close it
if (tcpClient) {
tcpClient.end();
ws.send("info: Disconnected from previous connection");
}
// Connect to the TCP server
tcpClient = net.createConnection(
{ host: ip, port: parseInt(port) },
() => {
ws.send(`info: Connected to ${ip}:${port}`);
}
);
tcpClient.on("data", (data) => {
console.info(`Received message from TCP server: ${data.toString()}`);
ws.send(`server: ${data.toString()}`);
});
tcpClient.on("end", () => {
ws.send("info: TCP connection closed");
});
tcpClient.on("error", (err) => {
ws.send(`error: ${err.message}`);
});
} else if (command === "disconnect") {
if (tcpClient) {
tcpClient.end();
ws.send("info: Disconnected from server");
tcpClient = null;
} else {
ws.send("error: No active connection to disconnect");
}
} else {
// Send message to the TCP server if connected
if (tcpClient) {
tcpClient.write(message + "\r\n", "ascii", () =>
console.info(`Sent message to TCP server: ${message}`)
);
} else {
ws.send("error: No active TCP connection");
}
}
});
ws.on("close", () => {
if (tcpClient) tcpClient.end();
});
});
console.log("WebSocket server running on ws://localhost:" + WEBSOCKET_PORT);