Update bot Client to work with the new connection logic
This commit is contained in:
parent
6a0b05df86
commit
157c8af4f4
@ -1,5 +1,5 @@
|
|||||||
import Hyperswarm from 'hyperswarm';
|
import Hyperswarm from 'hyperswarm';
|
||||||
import EventEmitter from 'node:events'
|
import EventEmitter from 'node:events';
|
||||||
import b4a from "b4a";
|
import b4a from "b4a";
|
||||||
|
|
||||||
class Client extends EventEmitter {
|
class Client extends EventEmitter {
|
||||||
@ -8,6 +8,7 @@ class Client extends EventEmitter {
|
|||||||
if (!botName) return console.error("BotName is not defined!");
|
if (!botName) return console.error("BotName is not defined!");
|
||||||
this.botName = botName;
|
this.botName = botName;
|
||||||
this.swarm = new Hyperswarm();
|
this.swarm = new Hyperswarm();
|
||||||
|
this.currentTopic = null; // Store the current topic
|
||||||
this.setupSwarm();
|
this.setupSwarm();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -15,7 +16,10 @@ class Client extends EventEmitter {
|
|||||||
this.swarm.on('connection', (peer) => {
|
this.swarm.on('connection', (peer) => {
|
||||||
peer.on('data', message => {
|
peer.on('data', message => {
|
||||||
const messageObj = JSON.parse(message.toString());
|
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 === "icon") this.emit('onIcon', peer, messageObj);
|
||||||
if (messageObj.type === "file") this.emit('onFile', 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}`);
|
console.log(`Connections count: ${this.swarm.connections.size} / Peers count: ${this.swarm.peers.size}`);
|
||||||
|
|
||||||
this.swarm.peers.forEach((peerInfo, peerId) => {
|
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 peer = [peerId];
|
||||||
const peerTopics = [peerInfo.topics]
|
const peerTopics = [peerInfo.topics]
|
||||||
.filter(topics => topics)
|
.filter(topics => topics)
|
||||||
@ -41,6 +43,7 @@ class Client extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
joinChatRoom(chatRoomID) {
|
joinChatRoom(chatRoomID) {
|
||||||
|
this.currentTopic = chatRoomID; // Store the current topic
|
||||||
this.discovery = this.swarm.join(Buffer.from(chatRoomID, 'hex'), { client: true, server: true });
|
this.discovery = this.swarm.join(Buffer.from(chatRoomID, 'hex'), { client: true, server: true });
|
||||||
this.discovery.flushed().then(() => {
|
this.discovery.flushed().then(() => {
|
||||||
console.log(`Bot ${this.botName} joined the chat room.`);
|
console.log(`Bot ${this.botName} joined the chat room.`);
|
||||||
@ -50,26 +53,28 @@ class Client extends EventEmitter {
|
|||||||
|
|
||||||
sendMessage(roomPeers, message) {
|
sendMessage(roomPeers, message) {
|
||||||
console.log('Bot name:', this.botName);
|
console.log('Bot name:', this.botName);
|
||||||
const timestamp = Date.now(); // Generate timestamp
|
const timestamp = Date.now();
|
||||||
const messageObj = {
|
const messageObj = {
|
||||||
type: 'message', // Add type field
|
type: 'message',
|
||||||
name: this.botName,
|
name: this.botName,
|
||||||
message,
|
message,
|
||||||
timestamp
|
timestamp,
|
||||||
|
topic: this.currentTopic // Include the current topic
|
||||||
};
|
};
|
||||||
const data = JSON.stringify(messageObj);
|
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);
|
for (const peer of peers) peer.write(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMessageToAll(message) {
|
sendMessageToAll(message) {
|
||||||
console.log('Bot name:', this.botName);
|
console.log('Bot name:', this.botName);
|
||||||
const timestamp = Date.now(); // Generate timestamp
|
const timestamp = Date.now();
|
||||||
const messageObj = {
|
const messageObj = {
|
||||||
type: 'message', // Add type field
|
type: 'message',
|
||||||
name: this.botName,
|
name: this.botName,
|
||||||
message,
|
message,
|
||||||
timestamp
|
timestamp,
|
||||||
|
topic: this.currentTopic // Include the current topic
|
||||||
};
|
};
|
||||||
const data = JSON.stringify(messageObj);
|
const data = JSON.stringify(messageObj);
|
||||||
const peers = [...this.swarm.connections];
|
const peers = [...this.swarm.connections];
|
||||||
|
Loading…
Reference in New Issue
Block a user