LinkUp-P2P-Chat/chatBot/includes/Client.js

91 lines
2.8 KiB
JavaScript
Raw Normal View History

2024-06-03 19:23:14 -04:00
import Hyperswarm from 'hyperswarm';
import EventEmitter from 'node:events';
import b4a from "b4a";
2024-06-03 19:23:14 -04:00
class Client extends EventEmitter {
constructor(botName) {
super();
2024-06-10 19:49:46 -04:00
if (!botName) return console.error("BotName is not defined!");
2024-06-03 19:23:14 -04:00
this.botName = botName;
this.swarm = new Hyperswarm();
this.currentTopic = null; // Store the current topic
2024-06-03 19:23:14 -04:00
this.setupSwarm();
}
setupSwarm() {
this.swarm.on('connection', (peer) => {
peer.on('data', message => {
2024-06-10 19:49:46 -04:00
const messageObj = JSON.parse(message.toString());
if (messageObj.type === "message") {
this.currentTopic = messageObj.topic; // Capture the topic from incoming messages
this.emit('onMessage', peer, messageObj);
}
2024-06-10 19:49:46 -04:00
if (messageObj.type === "icon") this.emit('onIcon', peer, messageObj);
if (messageObj.type === "file") this.emit('onFile', peer, messageObj);
});
2024-06-08 16:29:51 -04:00
peer.on('error', e => {
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-10 14:17:16 -04:00
this.swarm.peers.forEach((peerInfo, peerId) => {
const peer = [peerId];
2024-06-10 14:18:33 -04:00
const peerTopics = [peerInfo.topics]
2024-06-10 19:49:46 -04:00
.filter(topics => topics)
.map(topics => topics.map(topic => b4a.toString(topic, 'hex')));
});
2024-06-03 19:23:14 -04:00
});
}
joinChatRoom(chatRoomID) {
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.`);
this.emit('onBotJoinRoom');
2024-06-03 19:23:14 -04:00
});
}
sendMessage(roomPeers, message) {
2024-06-03 19:23:14 -04:00
console.log('Bot name:', this.botName);
const timestamp = Date.now();
2024-06-10 19:49:46 -04:00
const messageObj = {
type: 'message',
2024-06-10 19:49:46 -04:00
name: this.botName,
message,
timestamp,
topic: this.currentTopic // Include the current topic
2024-06-10 19:49:46 -04:00
};
const data = JSON.stringify(messageObj);
const peers = [...this.swarm.connections].filter(peer => roomPeers.includes(peer.remotePublicKey.toString('hex')));
2024-06-10 10:28:18 -04:00
for (const peer of peers) peer.write(data);
}
sendMessageToAll(message) {
console.log('Bot name:', this.botName);
const timestamp = Date.now();
2024-06-10 19:49:46 -04:00
const messageObj = {
type: 'message',
2024-06-10 19:49:46 -04:00
name: this.botName,
message,
timestamp,
topic: this.currentTopic // Include the current topic
2024-06-10 19:49:46 -04:00
};
const data = JSON.stringify(messageObj);
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.`);
}
}
export default Client;