Add nodeserver.js
This commit is contained in:
parent
36fb213612
commit
561d6c9b45
|
@ -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");
|
Loading…
Reference in New Issue