This commit is contained in:
Raven Scott 2024-06-22 16:49:42 -04:00
parent cd92618351
commit fd02cff23f

38
app.js
View File

@ -339,7 +339,15 @@ function handleIncomingMessage(messageObj) {
}
}
}
} else {
} else if (messageObj.type === 'guildJoin') {
const { guildTopic, guildData } = messageObj;
if (!config.guilds[guildTopic]) {
config.guilds[guildTopic] = guildData;
writeConfigToFile("./config.json");
renderGuildList();
joinGuild(guildTopic);
}
} else {
console.error('Received unknown message type:', messageObj);
}
}
@ -380,7 +388,19 @@ async function handleConnection(connection, info) {
connection.on('data', (data) => {
const messageObj = JSON.parse(data.toString());
eventEmitter.emit('onMessage', messageObj);
if (messageObj.type === 'guildJoin') {
const guildData = config.guilds[messageObj.guildTopic];
if (guildData) {
const guildJoinMessage = JSON.stringify({
type: 'guildJoin',
guildTopic: messageObj.guildTopic,
guildData: guildData
});
connection.write(guildJoinMessage);
}
} else {
eventEmitter.emit('onMessage', messageObj);
}
});
connection.on('close', () => {
@ -555,6 +575,20 @@ async function joinChatRoom(e) {
const roomTopicBuffer = b4a.from(roomTopic, 'hex');
addRoomToList(guildTopic, roomTopic);
await joinSwarm(roomTopicBuffer);
// Fetch guild data from a peer
const guildJoinMessage = JSON.stringify({
type: 'guildJoin',
guildTopic: guildTopic
});
const guildSwarm = activeRooms.find(room => room.topic === guildTopic);
if (guildSwarm) {
const peers = [...guildSwarm.swarm.connections];
for (const peer of peers) {
peer.write(guildJoinMessage);
}
}
}
async function joinSwarm(topicBuffer) {