websocket.js 708 octets
Newer Older
Quentin Vauthier's avatar
Quentin Vauthier a validé
const WebSocketServer = require('ws');
const dotenv = require('dotenv');
dotenv.config();
 
// Creating a new websocket server
const wss = new WebSocketServer.Server({ port: process.env.WS_PORT})
 
// Creating connection using websocket
wss.on("connection", ws => {
    ws.id = crypto.randomUUID()
    console.log("New client connected")
Quentin Vauthier's avatar
Quentin Vauthier a validé
    // handling what to do when clients disconnects from server
    ws.on("close", () => {
        console.log("Client disconnected")
Quentin Vauthier's avatar
Quentin Vauthier a validé
    });
    // handling client connection error
    ws.onerror = function () {
        console.log("Some WS Error occurred")
Quentin Vauthier's avatar
Quentin Vauthier a validé
    }
});
console.log(`The WebSocket server is running on port ${process.env.WS_PORT}`);
module.exports = wss;