Update bot Client to work with the new connection logic

This commit is contained in:
Raven Scott 2024-06-10 22:04:57 -04:00
parent 6a0b05df86
commit 157c8af4f4

View File

@ -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];