Compare commits
No commits in common. "dae080a833f3e48f6a8f22cde73ea5be1b9f467b" and "4c6cdb917098c35b6013fe547e9c03025f9356d6" have entirely different histories.
dae080a833
...
4c6cdb9170
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,3 @@
|
|||||||
node_modules
|
node_modules
|
||||||
package-lock.json
|
package-lock.json
|
||||||
shared-storage
|
storage
|
||||||
|
99
app.js
99
app.js
@ -4,19 +4,19 @@ import b4a from 'b4a';
|
|||||||
import ServeDrive from 'serve-drive';
|
import ServeDrive from 'serve-drive';
|
||||||
import Localdrive from 'localdrive';
|
import Localdrive from 'localdrive';
|
||||||
|
|
||||||
const sharedDrive = new Localdrive('./shared-storage');
|
const drive = new Localdrive('./storage');
|
||||||
let swarm;
|
let swarm;
|
||||||
let userName = 'Anonymous';
|
let userName = 'Anonymous';
|
||||||
let userAvatar = '';
|
let userAvatar = '';
|
||||||
|
let registeredUsers = JSON.parse(localStorage.getItem('registeredUsers')) || {};
|
||||||
let peerCount = 0;
|
let peerCount = 0;
|
||||||
|
|
||||||
async function initialize() {
|
async function initialize() {
|
||||||
swarm = new Hyperswarm();
|
swarm = new Hyperswarm();
|
||||||
|
|
||||||
await sharedDrive.ready();
|
await drive.ready();
|
||||||
|
|
||||||
const servePort = 1337;
|
const servePort = 1337;
|
||||||
const serve = new ServeDrive({ port: servePort, get: ({ key, filename, version }) => sharedDrive });
|
const serve = new ServeDrive({ port: servePort, get: ({ key, filename, version }) => drive });
|
||||||
await serve.ready();
|
await serve.ready();
|
||||||
console.log('Listening on http://localhost:' + serve.address().port);
|
console.log('Listening on http://localhost:' + serve.address().port);
|
||||||
|
|
||||||
@ -44,38 +44,6 @@ async function initialize() {
|
|||||||
messageForm.addEventListener('submit', sendMessage);
|
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');
|
const savedUser = localStorage.getItem('currentUser');
|
||||||
if (savedUser) {
|
if (savedUser) {
|
||||||
const user = JSON.parse(savedUser);
|
const user = JSON.parse(savedUser);
|
||||||
@ -92,8 +60,26 @@ async function initialize() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const randomTopic = crypto.randomBytes(32);
|
swarm.on('connection', (connection, info) => {
|
||||||
document.querySelector('#chat-room-topic').innerText = b4a.toString(randomTopic, 'hex');
|
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.');
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function updatePeerCount() {
|
function updatePeerCount() {
|
||||||
@ -103,17 +89,6 @@ 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) {
|
async function registerUser(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
@ -142,8 +117,9 @@ async function registerUser(e) {
|
|||||||
const reader = new FileReader();
|
const reader = new FileReader();
|
||||||
reader.onload = async () => {
|
reader.onload = async () => {
|
||||||
const fileBuffer = Buffer.from(reader.result);
|
const fileBuffer = Buffer.from(reader.result);
|
||||||
await sharedDrive.put(`/icons/${regUsername}-${file.name}`, fileBuffer);
|
await drive.put(`/icons/${regUsername}-${file.name}`, fileBuffer);
|
||||||
userAvatar = `http://localhost:1337/icons/${regUsername}-${file.name}`;
|
userAvatar = `http://localhost:1337/icons/${regUsername}-${file.name}`;
|
||||||
|
// Save avatar URL to localStorage
|
||||||
localStorage.setItem('avatarURL', userAvatar);
|
localStorage.setItem('avatarURL', userAvatar);
|
||||||
};
|
};
|
||||||
reader.readAsArrayBuffer(file);
|
reader.readAsArrayBuffer(file);
|
||||||
@ -173,6 +149,7 @@ async function continueRegistration(regUsername) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function createChatRoom() {
|
async function createChatRoom() {
|
||||||
|
// Generate a new random topic (32 byte string)
|
||||||
const topicBuffer = crypto.randomBytes(32);
|
const topicBuffer = crypto.randomBytes(32);
|
||||||
joinSwarm(topicBuffer);
|
joinSwarm(topicBuffer);
|
||||||
}
|
}
|
||||||
@ -188,6 +165,7 @@ async function joinSwarm(topicBuffer) {
|
|||||||
document.querySelector('#setup').classList.add('hidden');
|
document.querySelector('#setup').classList.add('hidden');
|
||||||
document.querySelector('#loading').classList.remove('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 });
|
const discovery = swarm.join(topicBuffer, { client: true, server: true });
|
||||||
await discovery.flushed();
|
await discovery.flushed();
|
||||||
|
|
||||||
@ -204,8 +182,7 @@ function sendMessage(e) {
|
|||||||
|
|
||||||
onMessageAdded(userName, message, userAvatar);
|
onMessageAdded(userName, message, userAvatar);
|
||||||
|
|
||||||
onMessageAdded(userName, message, userAvatar);
|
// Send the message to all peers (that you are connected to)
|
||||||
|
|
||||||
const messageObj = JSON.stringify({
|
const messageObj = JSON.stringify({
|
||||||
type: 'message',
|
type: 'message',
|
||||||
name: userName,
|
name: userName,
|
||||||
@ -220,23 +197,7 @@ function sendMessage(e) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function processFileUpdate(messageObj) {
|
// appends element to #messages element with content set to sender and message
|
||||||
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) {
|
function onMessageAdded(from, message, avatar) {
|
||||||
const $div = document.createElement('div');
|
const $div = document.createElement('div');
|
||||||
$div.classList.add('message');
|
$div.classList.add('message');
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 2.3 KiB |
Loading…
Reference in New Issue
Block a user