I actually dont know if it works or not. Changes: Replaced TopicBuffer with chatRoomID and now topicBuffer is being created directly in Client class; Replaced chatBot with Client; Client now extends EventEmitter; bot.on('onMessage', (pear, message) => {}) is now a thing instead of directly putting method in chatBot constructor

This commit is contained in:
MrMasrozYTLIVE
2024-06-08 22:34:25 +03:00
parent ccf34e9637
commit de00514397
3 changed files with 17 additions and 18 deletions

View File

@ -1,17 +1,18 @@
import Hyperswarm from 'hyperswarm';
import EventEmitter from 'node:events'
class chatBot {
constructor(topicBuffer, botName, onMessageReceived) {
this.topicBuffer = topicBuffer;
class Client extends EventEmitter {
constructor(chatRoomID, botName) {
super();
this.topicBuffer = Buffer.from(chatRoomID, 'hex');
this.botName = botName;
this.swarm = new Hyperswarm();
this.onMessageReceived = onMessageReceived;
this.setupSwarm();
}
setupSwarm() {
this.swarm.on('connection', (peer) => {
peer.on('data', message => this.onMessageReceived(peer, JSON.parse(message.toString())));
peer.on('data', message => this.emit("onMessage", peer, JSON.parse(message.toString())));
peer.on('error', e => console.log(`Connection error: ${e}`));
});
@ -41,4 +42,4 @@ class chatBot {
}
}
export default chatBot;
export default Client;