From 7ed88546b472a16808bc999e924ef0ad7c311fb3 Mon Sep 17 00:00:00 2001 From: louisa Date: Wed, 19 Jul 2023 18:13:06 +0200 Subject: [PATCH] testing online user display --- app.js | 24 ++++++++++--- public/index.html | 92 ++++++++++++++++++++++++++++++----------------- 2 files changed, 78 insertions(+), 38 deletions(-) diff --git a/app.js b/app.js index b07565a..00579bc 100644 --- a/app.js +++ b/app.js @@ -6,6 +6,7 @@ var path = require('path'); var server = http.createServer(app); var port = 8000; var osc = require('osc'); +var activeUsers = new Set(); // Active users Set var udpPort = new osc.UDPPort({ // This is the port we're listening on. @@ -21,13 +22,10 @@ var udpPort = new osc.UDPPort({ // Open the socket. udpPort.open(); - - const { SerialPort } = require('serialport') const { ReadlineParser } = require('@serialport/parser-readline') const sport = new SerialPort({ path: '/dev/ttyACM0', baudRate: 115200 }) - const parser = sport.pipe(new ReadlineParser({ delimiter: '\r\n' })) server.listen(port, () => { @@ -39,7 +37,13 @@ app.use(express.static(path.join(__dirname, "public"))); var io = require('socket.io')(server); io.on('connection', function(socket) { - console.log("A client connected!"); + console.log("new user online!"); + + // Add the new socket ID to the activeUsers Set + activeUsers.add(socket.id); + + // Emit the updated activeUsers Set to all connected clients + io.emit('activeUsers', Array.from(activeUsers)); parser.on('data', function(data) { const msg = data.split(' '); @@ -78,4 +82,14 @@ io.on('connection', function(socket) { socket.to('expo').emit(socket.id, msg); }); -}); + // Handle disconnection event + socket.on('disconnect', () => { + console.log("new user online:", socket.id); + + // Remove the disconnected socket ID from the activeUsers Set + activeUsers.delete(socket.id); + + // Emit the updated activeUsers Set to all connected clients + io.emit('activeUsers', Array.from(activeUsers)); + }); +}); \ No newline at end of file diff --git a/public/index.html b/public/index.html index 1570d26..2f6091c 100644 --- a/public/index.html +++ b/public/index.html @@ -19,6 +19,9 @@
+
+

stream

+
    @@ -34,8 +37,12 @@




    -

    OBJECTIVE:
    RUN

    +

    OBJECTIVE:
    You have lost your memory and wake up in a strange garage.

    +
    +

    users online:

    +
      +

      @@ -178,37 +185,38 @@ + } + + socket.on('chat message', function(msg) { + var item = document.createElement('li'); + item.textContent = msg; + item.className = 'others'; + messages.appendChild(item); + messages.scrollTop = messages.scrollHeight - messages.clientHeight; + }); + + socket.on('connect', function() { + // Add the current socket ID to the activeUsers array + activeUsers.push(socket.id); + updateActiveUsers(); + }); + + socket.on('disconnect', function() { + // Remove the disconnected socket ID from the activeUsers array + const index = activeUsers.indexOf(socket.id); + if (index !== -1) { + activeUsers.splice(index, 1); + updateActiveUsers(); + } + }); + + function updateActiveUsers() { + const userLog = document.getElementById('user-log'); + userLog.textContent = activeUsers.join(', '); + } + +