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
+