From 966250e88046710a630705f0a7b4932ffc9f5978 Mon Sep 17 00:00:00 2001 From: Rodrigo Ceccato Date: Tue, 6 Feb 2024 11:05:32 -0300 Subject: [PATCH] Add user count on endpoint online-users --- server.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/server.js b/server.js index 61faea3..c00cd37 100644 --- a/server.js +++ b/server.js @@ -6,6 +6,15 @@ const app = express(); const PORT = 35123; +// Object to store IP addresses and timestamps +const ipAddresses = {}; + +// Function to track IP addresses and timestamps +const trackIP = (ip) => { + ipAddresses[ip] = Date.now(); +}; + + app.use(cors()); app.use(bodyParser.json()); @@ -111,6 +120,8 @@ getPomoStateJSON = () => { // Define a route that serves the JSON app.get("/json-endpoint", cors(), (req, res) => { + const ip = req.ip || req.connection.remoteAddress; // Get IP address + trackIP(ip); // Track IP //console.log( //"Received request from: " + req.ip + " at " + new Date().toISOString() //); @@ -118,6 +129,29 @@ app.get("/json-endpoint", cors(), (req, res) => { res.json(getPomoStateJSON()); }); +// Endpoint to display the number of online users +app.get('/online-users', (req, res) => { + // Call the function to count unique IPs in the last 5 seconds + const onlineUsersCount = countUniqueIPsInLast5Seconds(); + + // Respond to the request with the online users count + res.send(`Number of online users in the last 5 seconds: ${onlineUsersCount}`); +}); + +// Function to count unique IPs in the last 5 seconds +const countUniqueIPsInLast5Seconds = () => { + const currentTime = Date.now(); + const threshold = currentTime - 5000; // 5 seconds ago + + const uniqueIPs = Object.keys(ipAddresses).filter( + (ip) => ipAddresses[ip] >= threshold + ); + + console.log('Number of unique IPs in the last 5 seconds:', uniqueIPs.length); + + return uniqueIPs.length; +}; + // Start index.html server to avoid mixed content error app.use(express.static(path.join(__dirname, '.'))); app.get('/', (req, res) => {