From 157c8af4f4417e43c22c60d501d0600da5ec8a7d Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Mon, 10 Jun 2024 22:04:57 -0400 Subject: [PATCH] Update bot Client to work with the new connection logic --- chatBot/includes/Client.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index 97f13fc..7c939c0 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -1,5 +1,5 @@ import Hyperswarm from 'hyperswarm'; -import EventEmitter from 'node:events' +import EventEmitter from 'node:events'; import b4a from "b4a"; class Client extends EventEmitter { @@ -8,6 +8,7 @@ class Client extends EventEmitter { if (!botName) return console.error("BotName is not defined!"); this.botName = botName; this.swarm = new Hyperswarm(); + this.currentTopic = null; // Store the current topic this.setupSwarm(); } @@ -15,7 +16,10 @@ class Client extends EventEmitter { this.swarm.on('connection', (peer) => { peer.on('data', message => { const messageObj = JSON.parse(message.toString()); - if (messageObj.type === "message") this.emit('onMessage', peer, messageObj); + if (messageObj.type === "message") { + this.currentTopic = messageObj.topic; // Capture the topic from incoming messages + this.emit('onMessage', peer, messageObj); + } if (messageObj.type === "icon") this.emit('onIcon', peer, messageObj); if (messageObj.type === "file") this.emit('onFile', peer, messageObj); }); @@ -30,8 +34,6 @@ class Client extends EventEmitter { console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`); this.swarm.peers.forEach((peerInfo, peerId) => { - // Please do not try to understand what is going on here. I have no idea anyway. But it surprisingly works - const peer = [peerId]; const peerTopics = [peerInfo.topics] .filter(topics => topics) @@ -41,6 +43,7 @@ class Client extends EventEmitter { } joinChatRoom(chatRoomID) { + 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.`); @@ -50,26 +53,28 @@ class Client extends EventEmitter { sendMessage(roomPeers, message) { console.log('Bot name:', this.botName); - const timestamp = Date.now(); // Generate timestamp + const timestamp = Date.now(); const messageObj = { - type: 'message', // Add type field + type: 'message', name: this.botName, message, - timestamp + timestamp, + topic: this.currentTopic // Include the current topic }; const data = JSON.stringify(messageObj); - const peers = [...this.swarm.connections].filter(peer => roomPeers.includes(peer.remotePublicKey.toString('hex'))); // We remove all the peers that aren't included in a room + const peers = [...this.swarm.connections].filter(peer => roomPeers.includes(peer.remotePublicKey.toString('hex'))); for (const peer of peers) peer.write(data); } sendMessageToAll(message) { console.log('Bot name:', this.botName); - const timestamp = Date.now(); // Generate timestamp + const timestamp = Date.now(); const messageObj = { - type: 'message', // Add type field + type: 'message', name: this.botName, message, - timestamp + timestamp, + topic: this.currentTopic // Include the current topic }; const data = JSON.stringify(messageObj); const peers = [...this.swarm.connections];