Compare commits

..

No commits in common. "114875cddbaf8e2cf5e0d593cf1f4d3f3d5c0cc2" and "e33244c1f12adf68274ea6ef023a22e2ad4ca82d" have entirely different histories.

38
app.js
View File

@ -6,7 +6,6 @@ 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);
@ -19,7 +18,6 @@ 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();
@ -66,17 +64,6 @@ async function initialize() {
registerDiv.classList.remove('hidden');
}
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);
}
});
swarm.on('connection', async (connection, info) => {
peerCount++;
updatePeerCount();
@ -93,9 +80,16 @@ async function initialize() {
connection.write(iconMessage);
}
connection.on('data', (data) => {
connection.on('data', async (data) => {
const messageObj = JSON.parse(data.toString());
eventEmitter.emit('onMessage', 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);
}
});
connection.on('close', () => {
@ -126,9 +120,9 @@ function registerUser(e) {
const avatarFile = document.querySelector('#avatar-file').files[0];
if (avatarFile) {
const reader = new FileReader();
reader.onload = async (event) => {
reader.onload = (event) => {
const buffer = new Uint8Array(event.target.result);
await drive.put(`/icons/${regUsername}.png`, buffer);
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));
@ -243,9 +237,9 @@ function sendMessage(e) {
}
function updatePeerCount() {
const peerCountElement = document.querySelector('#peers-count');
const peerCountElement = document.querySelector('#peer-count');
if (peerCountElement) {
peerCountElement.textContent = peerCount; // Display the actual peer count
peerCountElement.textContent = `Connected Peers: ${peerCount}`;
}
}
@ -289,11 +283,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) {
const avatarBlob = new Blob([avatarBuffer], { type: 'image/png' });
const avatarUrl = URL.createObjectURL(avatarBlob);
userIcon.src = avatarUrl;
userIcon.src = `http://localhost:1337/icons/${username}.png`; // Ensure the URL is correct
}
}