update peer count per topic

This commit is contained in:
Raven Scott 2024-06-11 00:18:27 -04:00
parent e57db27a13
commit 9aedd9fda4

23
app.js
View File

@ -15,7 +15,7 @@ await drive.ready();
let swarm; let swarm;
let registeredUsers = JSON.parse(localStorage.getItem('registeredUsers')) || {}; let registeredUsers = JSON.parse(localStorage.getItem('registeredUsers')) || {};
let peerCount = 0; let peerCounts = {}; // Change peerCount to an object
let activeRooms = []; let activeRooms = [];
const eventEmitter = new EventEmitter(); const eventEmitter = new EventEmitter();
@ -144,9 +144,13 @@ async function initialize() {
}); });
swarm.on('connection', async (connection, info) => { swarm.on('connection', async (connection, info) => {
peerCount++; const topic = info.topics[0].toString('hex');
updatePeerCount(); if (!peerCounts[topic]) {
console.log('Peer connected, current peer count:', peerCount); 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 // Send the current user's icon to the new peer
const iconBuffer = await drive.get(`/icons/${config.userName}.png`); const iconBuffer = await drive.get(`/icons/${config.userName}.png`);
@ -165,9 +169,9 @@ async function initialize() {
}); });
connection.on('close', () => { connection.on('close', () => {
peerCount--; peerCounts[topic]--;
updatePeerCount(); updatePeerCount(topic);
console.log('Peer disconnected, current peer count:', peerCount); console.log('Peer disconnected, current peer count for topic', topic, ':', peerCounts[topic]);
}); });
}); });
@ -321,6 +325,7 @@ function exitEditMode(roomItem, input, topic) {
function switchRoom(topic) { function switchRoom(topic) {
console.log('Switching to room:', topic); // Debugging log console.log('Switching to room:', topic); // Debugging log
document.querySelector('#chat-room-topic').innerText = topic; // Set full topic here document.querySelector('#chat-room-topic').innerText = topic; // Set full topic here
updatePeerCount(topic);
clearMessages(); clearMessages();
renderMessagesForRoom(topic); 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'); const peerCountElement = document.querySelector('#peers-count');
if (peerCountElement) { if (peerCountElement) {
peerCountElement.textContent = peerCount; // Display the actual peer count peerCountElement.textContent = peerCounts[topic] || 0; // Display the peer count for the specific topic
} }
} }