forked from snxraven/LinkUp-P2P-Chat
parent
9aedd9fda4
commit
7ec2901165
23
app.js
23
app.js
@ -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 peerCounts = {}; // Change peerCount to an object
|
let peerCount = 0;
|
||||||
let activeRooms = [];
|
let activeRooms = [];
|
||||||
const eventEmitter = new EventEmitter();
|
const eventEmitter = new EventEmitter();
|
||||||
|
|
||||||
@ -144,13 +144,9 @@ async function initialize() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
swarm.on('connection', async (connection, info) => {
|
swarm.on('connection', async (connection, info) => {
|
||||||
const topic = info.topics[0].toString('hex');
|
peerCount++;
|
||||||
if (!peerCounts[topic]) {
|
updatePeerCount();
|
||||||
peerCounts[topic] = 0;
|
console.log('Peer connected, current peer count:', peerCount);
|
||||||
}
|
|
||||||
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`);
|
||||||
@ -169,9 +165,9 @@ async function initialize() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
connection.on('close', () => {
|
connection.on('close', () => {
|
||||||
peerCounts[topic]--;
|
peerCount--;
|
||||||
updatePeerCount(topic);
|
updatePeerCount();
|
||||||
console.log('Peer disconnected, current peer count for topic', topic, ':', peerCounts[topic]);
|
console.log('Peer disconnected, current peer count:', peerCount);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -325,7 +321,6 @@ 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);
|
||||||
|
|
||||||
@ -495,10 +490,10 @@ function addFileMessage(from, fileName, fileUrl, fileType, avatar, topic) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updatePeerCount(topic) {
|
function updatePeerCount() {
|
||||||
const peerCountElement = document.querySelector('#peers-count');
|
const peerCountElement = document.querySelector('#peers-count');
|
||||||
if (peerCountElement) {
|
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