forked from snxraven/LinkUp-P2P-Chat
Changed lots of stuff inside of bot code. Got rid of peers in message object.
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
import Hyperswarm from 'hyperswarm';
|
||||
import EventEmitter from 'node:events';
|
||||
import b4a from "b4a";
|
||||
import TextMessage from "./TextMessage.js";
|
||||
import FileMessage from "./FileMessage.js";
|
||||
|
||||
class Client extends EventEmitter {
|
||||
constructor(botName) {
|
||||
@ -11,6 +13,10 @@ class Client extends EventEmitter {
|
||||
this.joinedRooms = new Set(); // Track the rooms the bot has joined
|
||||
this.currentTopic = null; // Track the current topic
|
||||
this.setupSwarm();
|
||||
|
||||
process.on('exit', () => {
|
||||
this.destroy();
|
||||
});
|
||||
}
|
||||
|
||||
setupSwarm() {
|
||||
@ -19,9 +25,14 @@ class Client extends EventEmitter {
|
||||
const messageObj = JSON.parse(message.toString());
|
||||
if (this.joinedRooms.has(messageObj.topic)) { // Process message only if it is from a joined room
|
||||
this.currentTopic = messageObj.topic; // Set the current topic from the incoming message
|
||||
if (messageObj.type === "message") this.emit('onMessage', peer, messageObj);
|
||||
if (messageObj.type === "icon") this.emit('onIcon', peer, messageObj);
|
||||
if (messageObj.type === "file") this.emit('onFile', peer, messageObj);
|
||||
if (messageObj.type === "message")
|
||||
this.emit('onMessage', peer, new TextMessage(messageObj.name, messageObj.avatar, messageObj.topic, messageObj.message, messageObj.timestamp));
|
||||
|
||||
if (messageObj.type === "file")
|
||||
this.emit('onFile', peer, new FileMessage(messageObj.name, messageObj.fileName, messageObj.fileUrl, messageObj.fileType, messageObj.avatar, messageObj.topic, messageObj.timestamp));
|
||||
|
||||
if (messageObj.type === "icon")
|
||||
this.emit('onIcon', peer, messageObj);
|
||||
}
|
||||
});
|
||||
|
||||
@ -46,18 +57,13 @@ class Client extends EventEmitter {
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: Make topic here actually work.
|
||||
sendMessage(topic, message) {
|
||||
sendTextMessage(message) {
|
||||
this.sendMessage(TextMessage.new(this, message));
|
||||
}
|
||||
|
||||
sendMessage(message) {
|
||||
console.log('Bot name:', this.botName);
|
||||
const timestamp = Date.now();
|
||||
const messageObj = {
|
||||
type: 'message',
|
||||
name: this.botName,
|
||||
message,
|
||||
timestamp,
|
||||
topic: this.currentTopic // Include the current topic
|
||||
};
|
||||
const data = JSON.stringify(messageObj);
|
||||
const data = message.toJsonString();
|
||||
const peers = [...this.swarm.connections];
|
||||
for (const peer of peers) peer.write(data);
|
||||
}
|
||||
|
@ -1,12 +1,26 @@
|
||||
class FileMessage {
|
||||
public FileMessage(chatRoom, userPeer, fileName, fileUrl, fileType, timestamp) {
|
||||
this.chatRoom = chatRoom;
|
||||
this.userPeer = userPeer;
|
||||
public FileMessage(peerName, fileName, fileUrl, fileType, peerAvatar, topic, timestamp) {
|
||||
this.peerName = peerName;
|
||||
this.fileName = fileName;
|
||||
this.fileUrl = fileUrl;
|
||||
this.fileType = fileType;
|
||||
this.peerAvatar = peerAvatar;
|
||||
this.topic = topic;
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public toJsonString() {
|
||||
return JSON.stringify({
|
||||
type: 'file',
|
||||
name: this.peerName,
|
||||
fileName: this.fileName,
|
||||
fileUrl: this.fileUrl,
|
||||
fileType: this.fileType,
|
||||
avatar: this.peerAvatar,
|
||||
topic: this.topic,
|
||||
timestamp: this.timestamp,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default FileMessage;
|
@ -1,10 +1,26 @@
|
||||
class TextMessage {
|
||||
public TextMessage(chatRoom, userPeer, message, timestamp) {
|
||||
this.chatRoom = chatRoom;
|
||||
this.userPeer = userPeer;
|
||||
public TextMessage(peerName, peerAvatar, topic, message, timestamp) {
|
||||
this.peerName = peerName;
|
||||
this.peerAvatar = peerAvatar;
|
||||
this.topic = topic;
|
||||
this.message = message;
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public toJsonString() {
|
||||
return JSON.stringify({
|
||||
type: 'message',
|
||||
name: this.peerName,
|
||||
message: this.message,
|
||||
avatar: this.peerAvatar,
|
||||
topic: this.topic,
|
||||
timestamp: this.timestamp
|
||||
});
|
||||
}
|
||||
|
||||
public static new(bot, message) {
|
||||
return new TextMessage(bot.botName, "", bot.currentTopic, message, Date.now());
|
||||
}
|
||||
}
|
||||
|
||||
export default TextMessage;
|
Reference in New Issue
Block a user