Fixed app.js not sending user ID itself and only peers it knows #5

Merged
snxraven merged 26 commits from MiTask/LinkUp-P2P-Chat:main into main 2024-06-10 11:26:59 -04:00
2 changed files with 10 additions and 3 deletions
Showing only changes of commit 8788adbc2b - Show all commits

View File

@ -61,7 +61,7 @@ loadCommands().then(commands => {
bot.on('onBotJoinRoom', () => {
console.log("Bot is ready!");
// bot.sendMessage(process.env.ON_READY_MESSAGE);
bot.sendMessageToAll(process.env.ON_READY_MESSAGE);
});
bot.joinChatRoom(process.env.LINKUP_ROOM_ID);

View File

@ -35,8 +35,15 @@ class Client extends EventEmitter {
sendMessage(roomPeers, message) {
console.log('Bot name:', this.botName);
const timestamp = Date.now(); // Generate timestamp
const peers = [...this.swarm.connections].filter(peer => roomPeers.includes(peer.remotePublicKey.toString('hex')));
console.log(`Sending message ${message} to peers ${peers}`)
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 data = JSON.stringify({name: this.botName, message, timestamp}); // Include timestamp
for (const peer of peers) peer.write(data);
}
sendMessageToAll(message) {
console.log('Bot name:', this.botName);
const timestamp = Date.now(); // Generate timestamp
const peers = [...this.swarm.connections]
const data = JSON.stringify({name: this.botName, message, timestamp}); // Include timestamp
for (const peer of peers) peer.write(data);
}