import Hyperswarm from 'hyperswarm'; import EventEmitter from 'node:events'; import b4a from "b4a"; import TextMessage from "./message/TextMessage.js"; import FileMessage from "./message/FileMessage.js"; import AudioMessage from "./message/AudioMessage.js"; import Message from "./message/Message.js"; import IconMessage from "./message/IconMessage.js"; class Client extends EventEmitter { constructor(botName) { super(); if (!botName) return console.error("Bot Name is not defined!"); this.botName = botName; this.botAvatar = ""; this.swarm = new Hyperswarm(); this.joinedRooms = new Set(); // Track the rooms the bot has joined this.currentTopic = null; // Track the current topic this.setupSwarm(); process.on('exit', () => { console.log('EXIT signal received. Shutting down HyperSwarm...'); this.destroy(); }); process.on('SIGTERM', async () => { console.log('SIGTERM signal received. Shutting down HyperSwarm...'); await this.destroy() console.log('HyperSwarm was shut down. Exiting the process with exit code 0.'); process.exit(0); }); process.on('SIGINT', async () => { console.log('SIGINT signal received. Shutting down HyperSwarm...'); await this.destroy() console.log('HyperSwarm was shut down. Exiting the process with exit code 0.'); process.exit(0); }); } async fetchAvatar(url) { this.botAvatar = url; const web = await fetch(url); const img = await web.body.getReader().read(); this.sendMessage(IconMessage.new(this, img.value)); } setupSwarm() { this.swarm.on('connection', (peer) => { peer.on('data', message => { const messageObj = JSON.parse(message.toString()); if (this.joinedRooms.has(messageObj.topic)) { // Process message only if it is from a joined room this.currentTopic = messageObj.topic; // Set the current topic from the incoming message const msgType = messageObj.type; const peerName = messageObj.name; const peerAvatar = messageObj.avatar; const timestamp = messageObj.timestamp; if (msgType === "message") this.emit('onMessage', peer, new TextMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.message)); if (msgType === "file") this.emit('onFile', peer, new FileMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.fileName, messageObj.fileUrl, messageObj.fileType)); if (msgType === "icon") this.emit('onIcon', peer, new IconMessage(peerName, peerAvatar, timestamp)); if (msgType === "audio") this.emit('onAudio', peer, new AudioMessage(peerName, peerAvatar, this.currentTopic, timestamp, messageObj.audio, messageObj.audioType)); } }); peer.on('error', e => { this.emit('onError', e); console.error(`Connection error: ${e}`) }); }); this.swarm.on('update', () => { console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`); }); } joinChatRoom(chatRoomID) { if (!chatRoomID || typeof chatRoomID !== 'string') { return console.error("Invalid chat room ID!"); } this.joinedRooms.add(chatRoomID); // Add the room to the list of joined rooms this.currentTopic = chatRoomID; // Store the current topic this.discovery = this.swarm.join(Buffer.from(chatRoomID, 'hex'), { client: true, server: true }); this.discovery.flushed().then(() => { console.log(`Bot ${this.botName} joined the chat room.`); this.emit('onBotJoinRoom'); }); } sendTextMessage(message) { this.sendMessage(TextMessage.new(this, message)); } sendMessage(message) { if(!(message instanceof Message)) return console.log(`message does not extend Message class (TextMessage, FileMessage, AudioMessage).`, message); // console.log('Bot name:', this.botName); console.log("Sending message:", message); const data = message.toJsonString(); const peers = [...this.swarm.connections]; for (const peer of peers) peer.write(data); } async destroy() { await this.swarm.destroy(); console.log(`Bot ${this.botName} disconnected.`); } } export default Client;