From fa2d93e830c8ad853c2ac072d3bb9b0832bf6158 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Mon, 3 Jun 2024 22:33:39 -0400 Subject: [PATCH] testing --- app.js | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index 1272852..3748317 100644 --- a/app.js +++ b/app.js @@ -4,8 +4,14 @@ import b4a from 'b4a'; import ServeDrive from 'serve-drive'; import Localdrive from 'localdrive'; import fs from 'fs'; +import Hyperdrive from 'hyperdrive'; +import Corestore from 'corestore'; + +const store = new Corestore('./storage'); +const drive = new Hyperdrive(store); + +await drive.ready(); -const drive = new Localdrive('./storage'); let swarm; let userName = 'Anonymous'; let userAvatar = ''; @@ -15,7 +21,6 @@ let peerCount = 0; async function initialize() { swarm = new Hyperswarm(); - await drive.ready(); const servePort = 1337; const serve = new ServeDrive({ port: servePort, get: ({ key, filename, version }) => drive }); await serve.ready(); @@ -61,9 +66,21 @@ async function initialize() { } } - swarm.on('connection', (connection, info) => { + swarm.on('connection', async (connection, info) => { peerCount++; updatePeerCount(); + + // Send the current user's icon to the new peer + const iconBuffer = await drive.get(`/icons/${userName}.png`); + if (iconBuffer) { + const iconMessage = JSON.stringify({ + type: 'icon', + username: userName, + avatar: iconBuffer.toString('base64'), + }); + connection.write(iconMessage); + } + connection.on('data', async (data) => { const messageObj = JSON.parse(data.toString()); if (messageObj.type === 'icon') { @@ -71,10 +88,12 @@ async function initialize() { 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); } }); + connection.on('close', () => { peerCount--; updatePeerCount(); @@ -129,6 +148,18 @@ async function registerUser(e) { userAvatar = `http://localhost:1337/icons/${regUsername}.png`; // Save avatar URL to localStorage localStorage.setItem('avatarURL', userAvatar); + + // Broadcast the icon to all connected peers + const iconMessage = JSON.stringify({ + type: 'icon', + username: regUsername, + avatar: fileBuffer.toString('base64'), + }); + + const peers = [...swarm.connections]; + for (const peer of peers) { + peer.write(iconMessage); + } }; reader.readAsArrayBuffer(file); } @@ -231,4 +262,13 @@ function onMessageAdded(from, message, avatar) { document.querySelector('#messages').appendChild($div); } -initialize(); \ No newline at end of file +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 = URL.createObjectURL(new Blob([avatarBuffer])); + } +} + +initialize();