From d0310247c5beeeaf00d35dc5f732180d91d2dacf Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Sat, 8 Jun 2024 16:08:53 -0400 Subject: [PATCH 1/3] Trying to fix peer counts --- app.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index 30eea35..49a8509 100644 --- a/app.js +++ b/app.js @@ -237,9 +237,9 @@ function sendMessage(e) { } function updatePeerCount() { - const peerCountElement = document.querySelector('#peer-count'); + const peerCountElement = document.querySelector('#peers-count'); if (peerCountElement) { - peerCountElement.textContent = `Connected Peers: ${peerCount}`; + peerCountElement.textContent = peerCount; // Display the actual peer count } } From c15b298268ff7f8a19503be2df3ff6bb92a575f3 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Sat, 8 Jun 2024 16:13:20 -0400 Subject: [PATCH 2/3] Tests --- app.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index 49a8509..06a3904 100644 --- a/app.js +++ b/app.js @@ -6,6 +6,7 @@ import Localdrive from 'localdrive'; import fs from 'fs'; import Hyperdrive from 'hyperdrive'; import Corestore from 'corestore'; +import { EventEmitter } from 'events'; const store = new Corestore('./storage'); const drive = new Hyperdrive(store); @@ -18,6 +19,7 @@ let userAvatar = ''; let registeredUsers = JSON.parse(localStorage.getItem('registeredUsers')) || {}; let peerCount = 0; let currentRoom = null; +const eventEmitter = new EventEmitter(); async function initialize() { swarm = new Hyperswarm(); @@ -64,6 +66,16 @@ async function initialize() { registerDiv.classList.remove('hidden'); } + eventEmitter.on('onMessage', (messageObj) => { + if (messageObj.type === 'icon') { + const username = messageObj.username; + const avatarBuffer = Buffer.from(messageObj.avatar, 'base64'); + updateIcon(username, avatarBuffer); + } else { + onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar); + } + }); + swarm.on('connection', async (connection, info) => { peerCount++; updatePeerCount(); @@ -80,16 +92,9 @@ async function initialize() { connection.write(iconMessage); } - connection.on('data', async (data) => { + connection.on('data', (data) => { const messageObj = JSON.parse(data.toString()); - if (messageObj.type === 'icon') { - const username = messageObj.username; - const avatarBuffer = Buffer.from(messageObj.avatar, 'base64'); - await drive.put(`/icons/${username}.png`, avatarBuffer); - updateIcon(username, avatarBuffer); - } else { - onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar); - } + eventEmitter.emit('onMessage', messageObj); }); connection.on('close', () => { From dd8099649fd750ac368f50f8895c01fd5dc63611 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Sat, 8 Jun 2024 16:15:55 -0400 Subject: [PATCH 3/3] Tests for user icons with eventEmitter --- app.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app.js b/app.js index 06a3904..fbe9779 100644 --- a/app.js +++ b/app.js @@ -66,10 +66,11 @@ async function initialize() { registerDiv.classList.remove('hidden'); } - eventEmitter.on('onMessage', (messageObj) => { + eventEmitter.on('onMessage', async (messageObj) => { if (messageObj.type === 'icon') { const username = messageObj.username; const avatarBuffer = Buffer.from(messageObj.avatar, 'base64'); + await drive.put(`/icons/${username}.png`, avatarBuffer); updateIcon(username, avatarBuffer); } else { onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar); @@ -125,9 +126,9 @@ function registerUser(e) { const avatarFile = document.querySelector('#avatar-file').files[0]; if (avatarFile) { const reader = new FileReader(); - reader.onload = (event) => { + reader.onload = async (event) => { const buffer = new Uint8Array(event.target.result); - drive.put(`/icons/${regUsername}.png`, buffer); + await drive.put(`/icons/${regUsername}.png`, buffer); userAvatar = `http://localhost:1337/icons/${regUsername}.png`; // Set the correct URL registeredUsers[regUsername] = userAvatar; localStorage.setItem('registeredUsers', JSON.stringify(registeredUsers)); @@ -288,11 +289,11 @@ function truncateHash(hash) { } async function updateIcon(username, avatarBuffer) { - // Update the icon in the local HTML if necessary - // This can be adjusted as per your needs const userIcon = document.querySelector(`img[src*="${username}.png"]`); if (userIcon) { - userIcon.src = `http://localhost:1337/icons/${username}.png`; // Ensure the URL is correct + const avatarBlob = new Blob([avatarBuffer], { type: 'image/png' }); + const avatarUrl = URL.createObjectURL(avatarBlob); + userIcon.src = avatarUrl; } }