From 9aedd9fda4979ab91b789803b0295d10bec8145b Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Tue, 11 Jun 2024 00:18:27 -0400 Subject: [PATCH] update peer count per topic --- app.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index d1a2bdd..4f2f9ac 100644 --- a/app.js +++ b/app.js @@ -15,7 +15,7 @@ await drive.ready(); let swarm; let registeredUsers = JSON.parse(localStorage.getItem('registeredUsers')) || {}; -let peerCount = 0; +let peerCounts = {}; // Change peerCount to an object let activeRooms = []; const eventEmitter = new EventEmitter(); @@ -144,9 +144,13 @@ async function initialize() { }); swarm.on('connection', async (connection, info) => { - peerCount++; - updatePeerCount(); - console.log('Peer connected, current peer count:', peerCount); + const topic = info.topics[0].toString('hex'); + if (!peerCounts[topic]) { + peerCounts[topic] = 0; + } + peerCounts[topic]++; + updatePeerCount(topic); + console.log('Peer connected, current peer count for topic', topic, ':', peerCounts[topic]); // Send the current user's icon to the new peer const iconBuffer = await drive.get(`/icons/${config.userName}.png`); @@ -165,9 +169,9 @@ async function initialize() { }); connection.on('close', () => { - peerCount--; - updatePeerCount(); - console.log('Peer disconnected, current peer count:', peerCount); + peerCounts[topic]--; + updatePeerCount(topic); + console.log('Peer disconnected, current peer count for topic', topic, ':', peerCounts[topic]); }); }); @@ -321,6 +325,7 @@ function exitEditMode(roomItem, input, topic) { function switchRoom(topic) { console.log('Switching to room:', topic); // Debugging log document.querySelector('#chat-room-topic').innerText = topic; // Set full topic here + updatePeerCount(topic); clearMessages(); renderMessagesForRoom(topic); @@ -490,10 +495,10 @@ function addFileMessage(from, fileName, fileUrl, fileType, avatar, topic) { } } -function updatePeerCount() { +function updatePeerCount(topic) { const peerCountElement = document.querySelector('#peers-count'); if (peerCountElement) { - peerCountElement.textContent = peerCount; // Display the actual peer count + peerCountElement.textContent = peerCounts[topic] || 0; // Display the peer count for the specific topic } }