Fixing bot messages

This commit is contained in:
Raven Scott 2024-06-10 19:49:46 -04:00
parent e1153cb5df
commit 01fee56692
2 changed files with 24 additions and 12 deletions

1
app.js
View File

@ -433,7 +433,6 @@ function onMessageAdded(from, message, avatar) {
const $img = document.createElement('img'); const $img = document.createElement('img');
$img.src = updatePortInUrl(avatar) || 'https://via.placeholder.com/40'; // Default to a placeholder image if avatar URL is not provided $img.src = updatePortInUrl(avatar) || 'https://via.placeholder.com/40'; // Default to a placeholder image if avatar URL is not provided
console.log(updatePortInUrl(avatar))
$img.classList.add('avatar'); $img.classList.add('avatar');
$div.appendChild($img); $div.appendChild($img);

View File

@ -14,9 +14,10 @@ class Client extends EventEmitter {
setupSwarm() { setupSwarm() {
this.swarm.on('connection', (peer) => { this.swarm.on('connection', (peer) => {
peer.on('data', message => { peer.on('data', message => {
if(message.type === "message") this.emit('onMessage', peer, JSON.parse(message.toString())); const messageObj = JSON.parse(message.toString());
if(message.type === "icon") this.emit('onIcon', peer, JSON.parse(message.toString())); if (messageObj.type === "message") this.emit('onMessage', peer, messageObj);
if(message.type === "file") this.emit('onFile', peer, JSON.parse(message.toString())); if (messageObj.type === "icon") this.emit('onIcon', peer, messageObj);
if (messageObj.type === "file") this.emit('onFile', peer, messageObj);
}); });
peer.on('error', e => { peer.on('error', e => {
@ -50,16 +51,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(); // Generate timestamp
const peers = [...this.swarm.connections].filter(peer => roomPeers.includes(peer.remotePublicKey.toString('hex'))); // We remove all the peers that arent included in a room const messageObj = {
const data = JSON.stringify({name: this.botName, message, timestamp}); // Include timestamp type: 'message', // Add type field
name: this.botName,
message,
timestamp
};
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
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(); // Generate timestamp
const peers = [...this.swarm.connections] const messageObj = {
const data = JSON.stringify({name: this.botName, message, timestamp}); // Include timestamp type: 'message', // Add type field
name: this.botName,
message,
timestamp
};
const data = JSON.stringify(messageObj);
const peers = [...this.swarm.connections];
for (const peer of peers) peer.write(data); for (const peer of peers) peer.write(data);
} }