LinkUp-P2P-Chat/app.js

308 lines
9.4 KiB
JavaScript
Raw Normal View History

2024-06-08 15:06:24 -04:00
import { EventEmitter } from 'events';
2024-06-03 19:23:14 -04:00
import Hyperswarm from 'hyperswarm';
import crypto from 'hypercore-crypto';
import b4a from 'b4a';
import ServeDrive from 'serve-drive';
import Localdrive from 'localdrive';
2024-06-03 20:04:15 -04:00
import fs from 'fs';
2024-06-03 22:33:39 -04:00
import Hyperdrive from 'hyperdrive';
import Corestore from 'corestore';
2024-06-03 22:33:39 -04:00
const store = new Corestore('./storage');
const drive = new Hyperdrive(store);
await drive.ready();
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
class ChatHandler extends EventEmitter {
constructor() {
super();
this.userName = 'Anonymous';
this.userAvatar = '';
this.registeredUsers = JSON.parse(localStorage.getItem('registeredUsers')) || {};
this.peerCount = 0;
this.currentRoom = null;
this.swarm = new Hyperswarm();
this.initialize();
2024-06-03 19:23:14 -04:00
}
2024-06-03 22:33:39 -04:00
2024-06-08 15:06:24 -04:00
async initialize() {
const servePort = 1337;
const serve = new ServeDrive({ port: servePort, get: ({ key, filename, version }) => drive });
await serve.ready();
console.log('Listening on http://localhost:' + serve.address().port);
const registerForm = document.querySelector('#register-form');
const selectAvatarButton = document.querySelector('#select-avatar');
const createChatRoomButton = document.querySelector('#create-chat-room');
const joinChatRoomButton = document.querySelector('#join-chat-room');
const messageForm = document.querySelector('#message-form');
const toggleSetupBtn = document.querySelector('#toggle-setup-btn');
const removeRoomBtn = document.querySelector('#remove-room-btn');
if (registerForm) {
registerForm.addEventListener('submit', (e) => this.registerUser(e));
}
if (selectAvatarButton) {
selectAvatarButton.addEventListener('click', () => {
document.querySelector('#avatar-file').click();
2024-06-03 22:33:39 -04:00
});
2024-06-08 15:06:24 -04:00
}
if (createChatRoomButton) {
createChatRoomButton.addEventListener('click', () => this.createChatRoom());
}
if (joinChatRoomButton) {
joinChatRoomButton.addEventListener('click', (e) => this.joinChatRoom(e));
}
if (messageForm) {
messageForm.addEventListener('submit', (e) => this.sendMessage(e));
}
if (toggleSetupBtn) {
toggleSetupBtn.addEventListener('click', () => this.toggleSetupView());
}
if (removeRoomBtn) {
removeRoomBtn.addEventListener('click', () => this.leaveRoom());
2024-06-03 22:33:39 -04:00
}
2024-06-08 15:06:24 -04:00
const registerDiv = document.querySelector('#register');
if (registerDiv) {
registerDiv.classList.remove('hidden');
}
this.swarm.on('connection', async (connection, info) => {
this.peerCount++;
this.updatePeerCount();
const iconBuffer = await drive.get(`/icons/${this.userName}.png`);
if (iconBuffer) {
const iconMessage = JSON.stringify({
type: 'icon',
username: this.userName,
avatar: iconBuffer.toString('base64'),
});
connection.write(iconMessage);
2024-06-03 19:53:10 -04:00
}
2024-06-08 15:06:24 -04:00
connection.on('data', (data) => this.emit('onMessage', data, connection));
connection.on('close', () => {
this.peerCount--;
this.updatePeerCount();
});
2024-06-03 19:43:33 -04:00
});
2024-06-03 22:33:39 -04:00
2024-06-08 15:06:24 -04:00
this.swarm.on('error', (err) => {
console.error('Swarm error:', err);
2024-06-03 19:43:33 -04:00
});
2024-06-03 19:42:30 -04:00
2024-06-08 15:06:24 -04:00
this.swarm.on('close', () => {
console.log('Swarm closed');
});
2024-06-03 19:42:30 -04:00
2024-06-08 15:06:24 -04:00
this.on('onMessage', async (data, connection) => this.handleMessage(data, connection));
}
2024-06-03 19:42:30 -04:00
2024-06-08 15:06:24 -04:00
async registerUser(e) {
e.preventDefault();
const regUsername = document.querySelector('#reg-username').value;
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
if (this.registeredUsers[regUsername]) {
alert('Username already taken. Please choose another.');
return;
}
const avatarFile = document.querySelector('#avatar-file').files[0];
if (avatarFile) {
const reader = new FileReader();
reader.onload = (event) => {
const buffer = new Uint8Array(event.target.result);
drive.put(`/icons/${regUsername}.png`, buffer);
this.userAvatar = URL.createObjectURL(new Blob([buffer]));
this.registeredUsers[regUsername] = this.userAvatar;
localStorage.setItem('registeredUsers', JSON.stringify(this.registeredUsers));
this.continueRegistration(regUsername);
};
reader.readAsArrayBuffer(avatarFile);
} else {
this.continueRegistration(regUsername);
}
2024-06-03 19:23:14 -04:00
}
2024-06-08 15:06:24 -04:00
async continueRegistration(regUsername) {
const loadingDiv = document.querySelector('#loading');
const setupDiv = document.querySelector('#setup');
if (!regUsername) {
alert('Please enter a username.');
return;
}
this.userName = regUsername;
setupDiv.classList.remove('hidden');
document.querySelector('#register').classList.add('hidden');
loadingDiv.classList.add('hidden');
const randomTopic = crypto.randomBytes(32);
document.querySelector('#chat-room-topic').innerText = this.truncateHash(b4a.toString(randomTopic, 'hex'));
2024-06-03 19:23:14 -04:00
}
2024-06-08 15:06:24 -04:00
async createChatRoom() {
const topicBuffer = crypto.randomBytes(32);
const topic = b4a.toString(topicBuffer, 'hex');
this.addRoomToList(topic);
this.joinSwarm(topicBuffer);
}
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
async joinChatRoom(e) {
e.preventDefault();
const topicStr = document.querySelector('#join-chat-room-topic').value;
const topicBuffer = b4a.from(topicStr, 'hex');
this.addRoomToList(topicStr);
this.joinSwarm(topicBuffer);
2024-06-03 19:23:14 -04:00
}
2024-06-08 15:06:24 -04:00
async joinSwarm(topicBuffer) {
if (this.currentRoom) {
this.currentRoom.destroy();
}
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
document.querySelector('#setup').classList.add('hidden');
document.querySelector('#loading').classList.remove('hidden');
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
const discovery = this.swarm.join(topicBuffer, { client: true, server: true });
await discovery.flushed();
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
const topic = b4a.toString(topicBuffer, 'hex');
document.querySelector('#chat-room-topic').innerText = topic; // Set full topic here
document.querySelector('#loading').classList.add('hidden');
document.querySelector('#chat').classList.remove('hidden');
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
this.currentRoom = discovery;
this.clearMessages();
}
2024-06-08 15:06:24 -04:00
addRoomToList(topic) {
const roomList = document.querySelector('#room-list');
const roomItem = document.createElement('li');
roomItem.textContent = this.truncateHash(topic);
roomItem.dataset.topic = topic;
roomItem.addEventListener('click', () => this.switchRoom(topic));
roomList.appendChild(roomItem);
}
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
switchRoom(topic) {
const topicBuffer = b4a.from(topic, 'hex');
this.joinSwarm(topicBuffer);
}
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
leaveRoom() {
if (this.currentRoom) {
const topic = b4a.toString(this.currentRoom.topic, 'hex');
const roomItem = document.querySelector(`li[data-topic="${topic}"]`);
if (roomItem) {
roomItem.remove();
}
this.currentRoom.destroy();
this.currentRoom = null;
}
document.querySelector('#chat').classList.add('hidden');
document.querySelector('#setup').classList.remove('hidden');
}
2024-06-08 15:06:24 -04:00
sendMessage(e) {
e.preventDefault();
const message = document.querySelector('#message').value;
document.querySelector('#message').value = '';
2024-06-08 15:06:24 -04:00
this.onMessageAdded(this.userName, message, this.userAvatar);
2024-06-08 15:06:24 -04:00
const messageObj = JSON.stringify({
type: 'message',
name: this.userName,
message,
avatar: this.userAvatar,
timestamp: Date.now(),
});
2024-06-08 15:06:24 -04:00
const peers = [...this.swarm.connections];
for (const peer of peers) {
peer.write(messageObj);
}
}
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
async handleMessage(data, connection) {
const messageObj = JSON.parse(data.toString());
if (messageObj.type === 'icon') {
const username = messageObj.username;
const avatarBuffer = Buffer.from(messageObj.avatar, 'base64');
await drive.put(`/icons/${username}.png`, avatarBuffer);
this.updateIcon(username, avatarBuffer);
} else {
this.onMessageAdded(messageObj.name, messageObj.message, messageObj.avatar);
}
}
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
scrollToBottom() {
var container = document.getElementById("messages-container");
container.scrollTop = container.scrollHeight;
2024-06-03 19:23:14 -04:00
}
2024-06-08 15:06:24 -04:00
onMessageAdded(from, message, avatar) {
const $div = document.createElement('div');
$div.classList.add('message');
2024-06-04 01:00:24 -04:00
2024-06-08 15:06:24 -04:00
const $img = document.createElement('img');
$img.src = avatar || 'https://via.placeholder.com/40';
$div.appendChild($img);
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
const $content = document.createElement('div');
$content.classList.add('message-content');
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
const $header = document.createElement('div');
$header.classList.add('message-header');
$header.textContent = from;
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
const $text = document.createElement('div');
$text.classList.add('message-text');
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
const md = window.markdownit();
const markdownContent = md.render(message);
$text.innerHTML = markdownContent;
2024-06-04 00:03:14 -04:00
2024-06-08 15:06:24 -04:00
$content.appendChild($header);
$content.appendChild($text);
$div.appendChild($content);
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
document.querySelector('#messages').appendChild($div);
this.scrollToBottom();
}
2024-06-03 19:23:14 -04:00
2024-06-08 15:06:24 -04:00
truncateHash(hash) {
return `${hash.slice(0, 6)}...${hash.slice(-6)}`;
}
2024-06-03 22:33:39 -04:00
2024-06-08 15:06:24 -04:00
async updateIcon(username, avatarBuffer) {
const userIcon = document.querySelector(`img[src*="${username}.png"]`);
if (userIcon) {
userIcon.src = URL.createObjectURL(new Blob([avatarBuffer]));
}
}
2024-06-08 15:06:24 -04:00
clearMessages() {
const messagesContainer = document.querySelector('#messages');
while (messagesContainer.firstChild) {
messagesContainer.removeChild(messagesContainer.firstChild);
}
2024-06-04 00:57:28 -04:00
}
2024-06-08 15:06:24 -04:00
toggleSetupView() {
const setupDiv = document.querySelector('#setup');
setupDiv.classList.toggle('hidden');
}
2024-06-08 15:06:24 -04:00
updatePeerCount() {
// Implement this method based on your needs to update the peer count in the UI
}
}
2024-06-08 15:06:24 -04:00
new ChatHandler();