From 561d6c9b45a18961ccb82d40eeac67b249848a9f Mon Sep 17 00:00:00 2001 From: heckeralt Date: Sun, 25 Aug 2024 18:27:19 -0400 Subject: [PATCH] Add nodeserver.js --- nodeserver.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 nodeserver.js diff --git a/nodeserver.js b/nodeserver.js new file mode 100644 index 0000000..bed6cb7 --- /dev/null +++ b/nodeserver.js @@ -0,0 +1,30 @@ +// Importing the required modules +const WebSocket = require('ws'); + +// Creating a new websocket server +const wss = new WebSocket.Server({ port: 8080 }); + +// Creating connection using websocket +wss.on("connection", (ws) => { + console.log("new client connected"); + + // sending message to client + ws.send('Welcome, you are connected!'); + + // on message from client + ws.on("message", (data) => { + console.log(`Client has sent us: ${data}`); + }); +ws.send("IP Generated!: " + Math.floor(Math.random() * 255) + "." + Math.floor(Math.random() * 255) + "." + Math.floor(Math.random() * 255) + "." + Math.floor(Math.random() * 255)) + // handling what to do when clients disconnects from server + ws.on("close", () => { + console.log("the client has disconnected"); + }); + + // handling client connection error + ws.onerror = function () { + console.log("Some Error occurred"); + }; +}); + +console.log("The WebSocket server is running on port 8080"); \ No newline at end of file