forked from snxraven/LinkUp-P2P-Chat
Fixing bot messages
This commit is contained in:
parent
e1153cb5df
commit
01fee56692
1
app.js
1
app.js
@ -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);
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ import b4a from "b4a";
|
|||||||
class Client extends EventEmitter {
|
class Client extends EventEmitter {
|
||||||
constructor(botName) {
|
constructor(botName) {
|
||||||
super();
|
super();
|
||||||
if(!botName) return console.error("BotName is not defined!");
|
if (!botName) return console.error("BotName is not defined!");
|
||||||
this.botName = botName;
|
this.botName = botName;
|
||||||
this.swarm = new Hyperswarm();
|
this.swarm = new Hyperswarm();
|
||||||
this.setupSwarm();
|
this.setupSwarm();
|
||||||
@ -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 => {
|
||||||
@ -40,7 +41,7 @@ class Client extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
joinChatRoom(chatRoomID) {
|
joinChatRoom(chatRoomID) {
|
||||||
this.discovery = this.swarm.join(Buffer.from(chatRoomID, 'hex'), {client: true, server: true});
|
this.discovery = this.swarm.join(Buffer.from(chatRoomID, 'hex'), { client: true, server: true });
|
||||||
this.discovery.flushed().then(() => {
|
this.discovery.flushed().then(() => {
|
||||||
console.log(`Bot ${this.botName} joined the chat room.`);
|
console.log(`Bot ${this.botName} joined the chat room.`);
|
||||||
this.emit('onBotJoinRoom');
|
this.emit('onBotJoinRoom');
|
||||||
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user