parent
9aedd9fda4
commit
7ec2901165
23
app.js
23
app.js
@ -15,7 +15,7 @@ await drive.ready();
|
||||
|
||||
let swarm;
|
||||
let registeredUsers = JSON.parse(localStorage.getItem('registeredUsers')) || {};
|
||||
let peerCounts = {}; // Change peerCount to an object
|
||||
let peerCount = 0;
|
||||
let activeRooms = [];
|
||||
const eventEmitter = new EventEmitter();
|
||||
|
||||
@ -144,13 +144,9 @@ async function initialize() {
|
||||
});
|
||||
|
||||
swarm.on('connection', async (connection, info) => {
|
||||
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]);
|
||||
peerCount++;
|
||||
updatePeerCount();
|
||||
console.log('Peer connected, current peer count:', peerCount);
|
||||
|
||||
// Send the current user's icon to the new peer
|
||||
const iconBuffer = await drive.get(`/icons/${config.userName}.png`);
|
||||
@ -169,9 +165,9 @@ async function initialize() {
|
||||
});
|
||||
|
||||
connection.on('close', () => {
|
||||
peerCounts[topic]--;
|
||||
updatePeerCount(topic);
|
||||
console.log('Peer disconnected, current peer count for topic', topic, ':', peerCounts[topic]);
|
||||
peerCount--;
|
||||
updatePeerCount();
|
||||
console.log('Peer disconnected, current peer count:', peerCount);
|
||||
});
|
||||
});
|
||||
|
||||
@ -325,7 +321,6 @@ 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);
|
||||
|
||||
@ -495,10 +490,10 @@ function addFileMessage(from, fileName, fileUrl, fileType, avatar, topic) {
|
||||
}
|
||||
}
|
||||
|
||||
function updatePeerCount(topic) {
|
||||
function updatePeerCount() {
|
||||
const peerCountElement = document.querySelector('#peers-count');
|
||||
if (peerCountElement) {
|
||||
peerCountElement.textContent = peerCounts[topic] || 0; // Display the peer count for the specific topic
|
||||
peerCountElement.textContent = peerCount; // Display the actual peer count
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user