import { WebSocket, WebSocketServer } from "ws"; import { createServer, Socket } from "net"; import { createWriter, Endian } from "bufferstuff"; const server = new WebSocketServer({ host: "0.0.0.0", port: 25567 }); server.on("listening", () => console.log("up")); let serverSocket:WebSocket|null = null; function writeServer(data:Buffer) { if (serverSocket) { serverSocket.send(data); } } let clientSocket:Socket|null = null; function writeClient(data:Buffer) { if (clientSocket) { clientSocket.write(data); } } const keepalivePacket = createWriter(Endian.BE, 1).writeByte(0).toBuffer(); server.on("connection", (socket) => { serverSocket = socket; console.log("Server is ready"); let interval = setInterval(() => { socket.send(keepalivePacket); }, 2000) socket.on("message", (data, isBinary) => { writeClient(data as Buffer); }); socket.on("error", () => { clearInterval(interval); }); socket.on("close", () => { clearInterval(interval); }); }); const clientServer = createServer((socket) => { clientSocket = socket; console.log("Client is ready"); socket.on("data", data => { writeServer(data); }); }); clientServer.listen(25565, () => console.log("Client server listening at 25565"));