forked from snxraven/LinkUp-P2P-Chat
testing
This commit is contained in:
parent
9bf319dcf3
commit
fa2d93e830
46
app.js
46
app.js
@ -4,8 +4,14 @@ import b4a from 'b4a';
|
|||||||
import ServeDrive from 'serve-drive';
|
import ServeDrive from 'serve-drive';
|
||||||
import Localdrive from 'localdrive';
|
import Localdrive from 'localdrive';
|
||||||
import fs from 'fs';
|
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 swarm;
|
||||||
let userName = 'Anonymous';
|
let userName = 'Anonymous';
|
||||||
let userAvatar = '';
|
let userAvatar = '';
|
||||||
@ -15,7 +21,6 @@ let peerCount = 0;
|
|||||||
async function initialize() {
|
async function initialize() {
|
||||||
swarm = new Hyperswarm();
|
swarm = new Hyperswarm();
|
||||||
|
|
||||||
await drive.ready();
|
|
||||||
const servePort = 1337;
|
const servePort = 1337;
|
||||||
const serve = new ServeDrive({ port: servePort, get: ({ key, filename, version }) => drive });
|
const serve = new ServeDrive({ port: servePort, get: ({ key, filename, version }) => drive });
|
||||||
await serve.ready();
|
await serve.ready();
|
||||||
@ -61,9 +66,21 @@ async function initialize() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
swarm.on('connection', (connection, info) => {
|
swarm.on('connection', async (connection, info) => {
|
||||||
peerCount++;
|
peerCount++;
|
||||||
updatePeerCount();
|
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) => {
|
connection.on('data', async (data) => {
|
||||||
const messageObj = JSON.parse(data.toString());
|
const messageObj = JSON.parse(data.toString());
|
||||||
if (messageObj.type === 'icon') {
|
if (messageObj.type === 'icon') {
|
||||||
@ -71,10 +88,12 @@ async function initialize() {
|
|||||||
const username = messageObj.username;
|
const username = messageObj.username;
|
||||||
const avatarBuffer = Buffer.from(messageObj.avatar, 'base64');
|
const avatarBuffer = Buffer.from(messageObj.avatar, 'base64');
|
||||||
await drive.put(`/icons/${username}.png`, avatarBuffer);
|
await drive.put(`/icons/${username}.png`, avatarBuffer);
|
||||||
|
updateIcon(username, avatarBuffer);
|
||||||
} else {
|
} else {
|
||||||
onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar);
|
onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
connection.on('close', () => {
|
connection.on('close', () => {
|
||||||
peerCount--;
|
peerCount--;
|
||||||
updatePeerCount();
|
updatePeerCount();
|
||||||
@ -129,6 +148,18 @@ async function registerUser(e) {
|
|||||||
userAvatar = `http://localhost:1337/icons/${regUsername}.png`;
|
userAvatar = `http://localhost:1337/icons/${regUsername}.png`;
|
||||||
// Save avatar URL to localStorage
|
// Save avatar URL to localStorage
|
||||||
localStorage.setItem('avatarURL', userAvatar);
|
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);
|
reader.readAsArrayBuffer(file);
|
||||||
}
|
}
|
||||||
@ -231,4 +262,13 @@ function onMessageAdded(from, message, avatar) {
|
|||||||
document.querySelector('#messages').appendChild($div);
|
document.querySelector('#messages').appendChild($div);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
initialize();
|
Loading…
Reference in New Issue
Block a user