Re adding per room topic counts
This commit is contained in:
parent
5edcff29e1
commit
0a3732fb07
109
app.js
109
app.js
@ -14,7 +14,6 @@ const drive = new Hyperdrive(store);
|
|||||||
await drive.ready();
|
await drive.ready();
|
||||||
|
|
||||||
let swarm;
|
let swarm;
|
||||||
let peerCount = 0;
|
|
||||||
let activeRooms = [];
|
let activeRooms = [];
|
||||||
const eventEmitter = new EventEmitter();
|
const eventEmitter = new EventEmitter();
|
||||||
|
|
||||||
@ -32,6 +31,12 @@ let config = {
|
|||||||
// Store messages for each room
|
// Store messages for each room
|
||||||
let messagesStore = {};
|
let messagesStore = {};
|
||||||
|
|
||||||
|
// Store peer counts for each room
|
||||||
|
let peerCounts = {};
|
||||||
|
|
||||||
|
// Map to store discovery instances and their associated topics
|
||||||
|
const discoveryTopicsMap = new Map();
|
||||||
|
|
||||||
// Function to get a random port between 49152 and 65535
|
// Function to get a random port between 49152 and 65535
|
||||||
function getRandomPort() {
|
function getRandomPort() {
|
||||||
return Math.floor(Math.random() * (65535 - 49152 + 1)) + 49152;
|
return Math.floor(Math.random() * (65535 - 49152 + 1)) + 49152;
|
||||||
@ -155,31 +160,47 @@ async function initialize() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
swarm.on('connection', async (connection, info) => {
|
swarm.on('connection', async (connection, info) => {
|
||||||
peerCount++;
|
try {
|
||||||
updatePeerCount();
|
const discovery = [...discoveryTopicsMap.entries()].find(([key, value]) => key.id === info.id);
|
||||||
console.log('Peer connected, current peer count:', peerCount);
|
const topic = discovery ? discovery[1] : null;
|
||||||
|
|
||||||
// Send the current user's icon to the new peer
|
if (!topic) {
|
||||||
const iconBuffer = await drive.get(`/icons/${config.userName}.png`);
|
console.error('No topic found in connection info:', info);
|
||||||
if (iconBuffer) {
|
return;
|
||||||
const iconMessage = JSON.stringify({
|
}
|
||||||
type: 'icon',
|
|
||||||
username: config.userName,
|
if (!peerCounts[topic]) {
|
||||||
avatar: b4a.toString(iconBuffer, 'base64'),
|
peerCounts[topic] = 0;
|
||||||
|
}
|
||||||
|
peerCounts[topic]++;
|
||||||
|
updatePeerCount(topic);
|
||||||
|
console.log('Peer connected, current peer count:', peerCounts[topic]);
|
||||||
|
|
||||||
|
// Send the current user's icon to the new peer
|
||||||
|
const iconBuffer = await drive.get(`/icons/${config.userName}.png`);
|
||||||
|
if (iconBuffer) {
|
||||||
|
const iconMessage = JSON.stringify({
|
||||||
|
type: 'icon',
|
||||||
|
username: config.userName,
|
||||||
|
avatar: b4a.toString(iconBuffer, 'base64'),
|
||||||
|
topic
|
||||||
|
});
|
||||||
|
connection.write(iconMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
connection.on('data', (data) => {
|
||||||
|
const messageObj = JSON.parse(data.toString());
|
||||||
|
eventEmitter.emit('onMessage', messageObj);
|
||||||
});
|
});
|
||||||
connection.write(iconMessage);
|
|
||||||
|
connection.on('close', () => {
|
||||||
|
peerCounts[topic]--;
|
||||||
|
updatePeerCount(topic);
|
||||||
|
console.log('Peer disconnected, current peer count:', peerCounts[topic]);
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error handling connection:', error);
|
||||||
}
|
}
|
||||||
|
|
||||||
connection.on('data', (data) => {
|
|
||||||
const messageObj = JSON.parse(data.toString());
|
|
||||||
eventEmitter.emit('onMessage', messageObj);
|
|
||||||
});
|
|
||||||
|
|
||||||
connection.on('close', () => {
|
|
||||||
peerCount--;
|
|
||||||
updatePeerCount();
|
|
||||||
console.log('Peer disconnected, current peer count:', peerCount);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
swarm.on('error', (err) => {
|
swarm.on('error', (err) => {
|
||||||
@ -330,6 +351,10 @@ async function joinSwarm(topicBuffer) {
|
|||||||
await discovery.flushed();
|
await discovery.flushed();
|
||||||
|
|
||||||
activeRooms.push({ topic, discovery });
|
activeRooms.push({ topic, discovery });
|
||||||
|
peerCounts[topic] = 0; // Initialize peer count for the new room
|
||||||
|
|
||||||
|
// Store the topic in the map
|
||||||
|
discoveryTopicsMap.set(discovery, topic);
|
||||||
|
|
||||||
console.log('Joined room:', topic); // Debugging log
|
console.log('Joined room:', topic); // Debugging log
|
||||||
|
|
||||||
@ -343,9 +368,13 @@ async function joinSwarm(topicBuffer) {
|
|||||||
function addRoomToList(topic) {
|
function addRoomToList(topic) {
|
||||||
const roomList = document.querySelector('#room-list');
|
const roomList = document.querySelector('#room-list');
|
||||||
const roomItem = document.createElement('li');
|
const roomItem = document.createElement('li');
|
||||||
roomItem.textContent = truncateHash(topic);
|
|
||||||
roomItem.dataset.topic = topic;
|
roomItem.dataset.topic = topic;
|
||||||
|
|
||||||
|
const roomName = document.createElement('span');
|
||||||
|
roomName.textContent = truncateHash(topic);
|
||||||
|
|
||||||
|
roomItem.appendChild(roomName);
|
||||||
|
|
||||||
roomItem.addEventListener('dblclick', () => enterEditMode(roomItem));
|
roomItem.addEventListener('dblclick', () => enterEditMode(roomItem));
|
||||||
roomItem.addEventListener('click', () => switchRoom(topic));
|
roomItem.addEventListener('click', () => switchRoom(topic));
|
||||||
roomList.appendChild(roomItem);
|
roomList.appendChild(roomItem);
|
||||||
@ -433,6 +462,9 @@ function switchRoom(topic) {
|
|||||||
// Show chat UI elements
|
// Show chat UI elements
|
||||||
document.querySelector('#chat').classList.remove('hidden');
|
document.querySelector('#chat').classList.remove('hidden');
|
||||||
document.querySelector('#setup').classList.add('hidden');
|
document.querySelector('#setup').classList.add('hidden');
|
||||||
|
|
||||||
|
// Update the peer count in the header
|
||||||
|
updateHeaderPeerCount(topic);
|
||||||
}
|
}
|
||||||
|
|
||||||
function leaveRoom(topic) {
|
function leaveRoom(topic) {
|
||||||
@ -441,6 +473,7 @@ function leaveRoom(topic) {
|
|||||||
const room = activeRooms[roomIndex];
|
const room = activeRooms[roomIndex];
|
||||||
room.discovery.destroy();
|
room.discovery.destroy();
|
||||||
activeRooms.splice(roomIndex, 1);
|
activeRooms.splice(roomIndex, 1);
|
||||||
|
discoveryTopicsMap.delete(room.discovery);
|
||||||
}
|
}
|
||||||
|
|
||||||
const roomItem = document.querySelector(`li[data-topic="${topic}"]`);
|
const roomItem = document.querySelector(`li[data-topic="${topic}"]`);
|
||||||
@ -568,10 +601,32 @@ function addFileMessage(from, fileName, fileUrl, fileType, avatar, topic) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updatePeerCount() {
|
function updatePeerCount(topic) {
|
||||||
|
let peerCountElement = document.querySelector(`#peers-count-${topic}`);
|
||||||
|
if (!peerCountElement) {
|
||||||
|
const roomItem = document.querySelector(`#room-list li[data-topic="${topic}"]`);
|
||||||
|
if (roomItem) {
|
||||||
|
peerCountElement = document.createElement('span');
|
||||||
|
peerCountElement.id = `peers-count-${topic}`;
|
||||||
|
// roomItem.appendChild(peerCountElement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (peerCountElement) {
|
||||||
|
peerCountElement.textContent = ` (${peerCounts[topic]})`; // Display the peer count for the specific topic
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the header peer count if the current room matches the topic
|
||||||
|
const currentTopic = document.querySelector('#chat-room-topic').innerText;
|
||||||
|
if (currentTopic === topic) {
|
||||||
|
updateHeaderPeerCount(topic);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateHeaderPeerCount(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
|
const peerCount = peerCounts[topic] || 0;
|
||||||
|
peerCountElement.textContent = peerCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -759,4 +814,4 @@ function addMessageToStore(topic, messageObj) {
|
|||||||
// Call this function when loading the rooms initially
|
// Call this function when loading the rooms initially
|
||||||
renderRoomList();
|
renderRoomList();
|
||||||
|
|
||||||
initialize();
|
initialize();
|
Loading…
Reference in New Issue
Block a user