forked from snxraven/LinkUp-P2P-Chat
test
This commit is contained in:
parent
4c6cdb9170
commit
50026db476
14
app.js
14
app.js
@ -3,6 +3,7 @@ import crypto from 'hypercore-crypto';
|
|||||||
import b4a from 'b4a';
|
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';
|
||||||
|
|
||||||
const drive = new Localdrive('./storage');
|
const drive = new Localdrive('./storage');
|
||||||
let swarm;
|
let swarm;
|
||||||
@ -63,9 +64,16 @@ async function initialize() {
|
|||||||
swarm.on('connection', (connection, info) => {
|
swarm.on('connection', (connection, info) => {
|
||||||
peerCount++;
|
peerCount++;
|
||||||
updatePeerCount();
|
updatePeerCount();
|
||||||
connection.on('data', (data) => {
|
connection.on('data', async (data) => {
|
||||||
const messageObj = JSON.parse(data.toString());
|
const messageObj = JSON.parse(data.toString());
|
||||||
|
if (messageObj.type === 'icon') {
|
||||||
|
// Save icon to local directory
|
||||||
|
const username = messageObj.username;
|
||||||
|
const avatarBuffer = Buffer.from(messageObj.avatar, 'base64');
|
||||||
|
await drive.put(`/icons/${username}.png`, avatarBuffer);
|
||||||
|
} else {
|
||||||
onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar);
|
onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
connection.on('close', () => {
|
connection.on('close', () => {
|
||||||
peerCount--;
|
peerCount--;
|
||||||
@ -117,8 +125,8 @@ 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 drive.put(`/icons/${regUsername}-${file.name}`, fileBuffer);
|
await drive.put(`/icons/${regUsername}.png`, fileBuffer);
|
||||||
userAvatar = `http://localhost:1337/icons/${regUsername}-${file.name}`;
|
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);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user