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 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
}
}