Compare commits

..

3 Commits

Author SHA1 Message Date
Raven Scott
dae080a833 test 2024-06-03 20:01:48 -04:00
Raven Scott
2fbc3f3bf0 test 2024-06-03 20:01:09 -04:00
Raven Scott
50026db476 test 2024-06-03 19:53:10 -04:00
3 changed files with 70 additions and 31 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
node_modules
package-lock.json
storage
shared-storage

99
app.js
View File

@ -4,19 +4,19 @@ import b4a from 'b4a';
import ServeDrive from 'serve-drive';
import Localdrive from 'localdrive';
const drive = new Localdrive('./storage');
const sharedDrive = new Localdrive('./shared-storage');
let swarm;
let userName = 'Anonymous';
let userAvatar = '';
let registeredUsers = JSON.parse(localStorage.getItem('registeredUsers')) || {};
let peerCount = 0;
async function initialize() {
swarm = new Hyperswarm();
await drive.ready();
await sharedDrive.ready();
const servePort = 1337;
const serve = new ServeDrive({ port: servePort, get: ({ key, filename, version }) => drive });
const serve = new ServeDrive({ port: servePort, get: ({ key, filename, version }) => sharedDrive });
await serve.ready();
console.log('Listening on http://localhost:' + serve.address().port);
@ -44,6 +44,38 @@ async function initialize() {
messageForm.addEventListener('submit', sendMessage);
}
swarm.on('connection', (connection, info) => {
peerCount++;
updatePeerCount();
connection.on('data', async (data) => {
const messageObj = JSON.parse(data.toString());
if (messageObj.type === 'avatarRequest') {
const avatarBuffer = await getAvatar(messageObj.username);
if (avatarBuffer) {
connection.write(avatarBuffer);
}
} else if (messageObj.type === 'fileUpdate') {
await processFileUpdate(messageObj);
} else {
onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar);
}
});
connection.on('close', () => {
peerCount--;
updatePeerCount();
});
});
swarm.on('error', (err) => {
console.error('Swarm error:', err);
});
swarm.on('close', () => {
console.log('Swarm closed.');
});
const savedUser = localStorage.getItem('currentUser');
if (savedUser) {
const user = JSON.parse(savedUser);
@ -60,26 +92,8 @@ async function initialize() {
}
}
swarm.on('connection', (connection, info) => {
peerCount++;
updatePeerCount();
connection.on('data', (data) => {
const messageObj = JSON.parse(data.toString());
onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar);
});
connection.on('close', () => {
peerCount--;
updatePeerCount();
});
});
swarm.on('error', (err) => {
console.error('Swarm error:', err);
});
swarm.on('close', () => {
console.log('Swarm closed.');
});
const randomTopic = crypto.randomBytes(32);
document.querySelector('#chat-room-topic').innerText = b4a.toString(randomTopic, 'hex');
}
function updatePeerCount() {
@ -89,6 +103,17 @@ function updatePeerCount() {
}
}
async function getAvatar(username) {
try {
const avatarPath = `./shared-storage/icons/${username}.png`;
const avatarBuffer = await sharedDrive.get(avatarPath);
return avatarBuffer;
} catch (error) {
console.error('Error getting avatar:', error);
return null;
}
}
async function registerUser(e) {
e.preventDefault();
@ -117,9 +142,8 @@ async function registerUser(e) {
const reader = new FileReader();
reader.onload = async () => {
const fileBuffer = Buffer.from(reader.result);
await drive.put(`/icons/${regUsername}-${file.name}`, fileBuffer);
await sharedDrive.put(`/icons/${regUsername}-${file.name}`, fileBuffer);
userAvatar = `http://localhost:1337/icons/${regUsername}-${file.name}`;
// Save avatar URL to localStorage
localStorage.setItem('avatarURL', userAvatar);
};
reader.readAsArrayBuffer(file);
@ -149,7 +173,6 @@ async function continueRegistration(regUsername) {
}
async function createChatRoom() {
// Generate a new random topic (32 byte string)
const topicBuffer = crypto.randomBytes(32);
joinSwarm(topicBuffer);
}
@ -165,7 +188,6 @@ async function joinSwarm(topicBuffer) {
document.querySelector('#setup').classList.add('hidden');
document.querySelector('#loading').classList.remove('hidden');
// Join the swarm with the topic. Setting both client/server to true means that this app can act as both.
const discovery = swarm.join(topicBuffer, { client: true, server: true });
await discovery.flushed();
@ -182,7 +204,8 @@ function sendMessage(e) {
onMessageAdded(userName, message, userAvatar);
// Send the message to all peers (that you are connected to)
onMessageAdded(userName, message, userAvatar);
const messageObj = JSON.stringify({
type: 'message',
name: userName,
@ -197,7 +220,23 @@ function sendMessage(e) {
}
}
// appends element to #messages element with content set to sender and message
async function processFileUpdate(messageObj) {
try {
const filePath = messageObj.filePath;
const fileBuffer = await downloadFileFromPeer(messageObj.peerId, filePath);
await sharedDrive.put(filePath, fileBuffer);
console.log(`File ${filePath} updated successfully.`);
} catch (error) {
console.error('Error processing file update:', error);
}
}
async function downloadFileFromPeer(peerId, filePath) {
// Implement logic to download file from peer using Hyperswarm or other P2P mechanisms
}
function onMessageAdded(from, message, avatar) {
const $div = document.createElement('div');
$div.classList.add('message');

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB