forked from snxraven/LinkUp-P2P-Chat
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:
@ -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;
|
Reference in New Issue
Block a user