2024-06-03 19:23:14 -04:00
|
|
|
import Hyperswarm from 'hyperswarm';
|
2024-06-10 22:04:57 -04:00
|
|
|
import EventEmitter from 'node:events';
|
2024-06-10 14:09:33 -04:00
|
|
|
import b4a from "b4a";
|
2024-06-11 14:47:42 -04:00
|
|
|
import TextMessage from "./TextMessage.js";
|
|
|
|
import FileMessage from "./FileMessage.js";
|
2024-06-03 19:23:14 -04:00
|
|
|
|
2024-06-08 15:34:25 -04:00
|
|
|
class Client extends EventEmitter {
|
2024-06-09 03:51:15 -04:00
|
|
|
constructor(botName) {
|
2024-06-08 15:34:25 -04:00
|
|
|
super();
|
2024-06-11 13:53:30 -04:00
|
|
|
if(!botName) return console.error("Bot Name is not defined!");
|
2024-06-03 19:23:14 -04:00
|
|
|
this.botName = botName;
|
|
|
|
this.swarm = new Hyperswarm();
|
2024-06-10 22:20:36 -04:00
|
|
|
this.joinedRooms = new Set(); // Track the rooms the bot has joined
|
|
|
|
this.currentTopic = null; // Track the current topic
|
2024-06-03 19:23:14 -04:00
|
|
|
this.setupSwarm();
|
2024-06-11 14:47:42 -04:00
|
|
|
|
|
|
|
process.on('exit', () => {
|
|
|
|
this.destroy();
|
|
|
|
});
|
2024-06-03 19:23:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
setupSwarm() {
|
|
|
|
this.swarm.on('connection', (peer) => {
|
2024-06-10 14:23:59 -04:00
|
|
|
peer.on('data', message => {
|
2024-06-10 19:49:46 -04:00
|
|
|
const messageObj = JSON.parse(message.toString());
|
2024-06-10 22:20:36 -04:00
|
|
|
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
|
2024-06-11 14:47:42 -04:00
|
|
|
if (messageObj.type === "message")
|
|
|
|
this.emit('onMessage', peer, new TextMessage(messageObj.name, messageObj.avatar, messageObj.topic, messageObj.message, messageObj.timestamp));
|
|
|
|
|
|
|
|
if (messageObj.type === "file")
|
|
|
|
this.emit('onFile', peer, new FileMessage(messageObj.name, messageObj.fileName, messageObj.fileUrl, messageObj.fileType, messageObj.avatar, messageObj.topic, messageObj.timestamp));
|
|
|
|
|
|
|
|
if (messageObj.type === "icon")
|
|
|
|
this.emit('onIcon', peer, messageObj);
|
2024-06-10 22:04:57 -04:00
|
|
|
}
|
2024-06-10 14:23:59 -04:00
|
|
|
});
|
|
|
|
|
2024-06-08 16:29:51 -04:00
|
|
|
peer.on('error', e => {
|
2024-06-08 16:31:24 -04:00
|
|
|
this.emit('onError', e);
|
2024-06-08 16:29:51 -04:00
|
|
|
console.error(`Connection error: ${e}`)
|
|
|
|
});
|
2024-06-03 19:23:14 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
this.swarm.on('update', () => {
|
2024-06-10 13:57:32 -04:00
|
|
|
console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`);
|
2024-06-03 19:23:14 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-09 03:51:15 -04:00
|
|
|
joinChatRoom(chatRoomID) {
|
2024-06-10 22:20:36 -04:00
|
|
|
this.joinedRooms.add(chatRoomID); // Add the room to the list of joined rooms
|
2024-06-10 22:04:57 -04:00
|
|
|
this.currentTopic = chatRoomID; // Store the current topic
|
2024-06-10 19:49:46 -04:00
|
|
|
this.discovery = this.swarm.join(Buffer.from(chatRoomID, 'hex'), { client: true, server: true });
|
2024-06-03 19:23:14 -04:00
|
|
|
this.discovery.flushed().then(() => {
|
|
|
|
console.log(`Bot ${this.botName} joined the chat room.`);
|
2024-06-08 16:36:00 -04:00
|
|
|
this.emit('onBotJoinRoom');
|
2024-06-03 19:23:14 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-11 14:47:42 -04:00
|
|
|
sendTextMessage(message) {
|
|
|
|
this.sendMessage(TextMessage.new(this, message));
|
|
|
|
}
|
|
|
|
|
|
|
|
sendMessage(message) {
|
2024-06-10 10:28:18 -04:00
|
|
|
console.log('Bot name:', this.botName);
|
2024-06-11 14:47:42 -04:00
|
|
|
const data = message.toJsonString();
|
2024-06-10 19:49:46 -04:00
|
|
|
const peers = [...this.swarm.connections];
|
2024-06-03 19:23:14 -04:00
|
|
|
for (const peer of peers) peer.write(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
this.swarm.destroy();
|
|
|
|
console.log(`Bot ${this.botName} disconnected.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-08 16:31:24 -04:00
|
|
|
export default Client;
|