Feat: Adding GUILDS! #12
43
app.js
43
app.js
@ -179,6 +179,11 @@ async function initialize() {
|
||||
console.log('Event switchRoom with roomTopic:', roomTopic);
|
||||
switchRoom(guildTopic, roomTopic);
|
||||
});
|
||||
|
||||
document.addEventListener('joinGuildRequest', (event) => {
|
||||
const { guildTopic } = event.detail;
|
||||
joinGuildRequest(guildTopic);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error during initialization:', error);
|
||||
}
|
||||
@ -195,6 +200,7 @@ function setupEventListeners() {
|
||||
const attachFileButton = document.getElementById('attach-file');
|
||||
const fileInput = document.getElementById('file-input');
|
||||
const talkButton = document.getElementById('talk-btn');
|
||||
const joinGuildBtn = document.getElementById('join-guild');
|
||||
|
||||
if (registerForm) {
|
||||
registerForm.addEventListener('submit', registerUser);
|
||||
@ -234,6 +240,14 @@ function setupEventListeners() {
|
||||
if (talkButton) {
|
||||
setupTalkButton();
|
||||
}
|
||||
if (joinGuildBtn) {
|
||||
joinGuildBtn.addEventListener('click', () => {
|
||||
const guildTopic = document.getElementById('join-guild-topic').value.trim();
|
||||
if (guildTopic) {
|
||||
joinGuildRequest(guildTopic);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Add event listeners only for room items
|
||||
document.querySelectorAll('.room-item').forEach(item => {
|
||||
@ -1207,4 +1221,33 @@ function handleFileInput(event) {
|
||||
}
|
||||
}
|
||||
|
||||
async function joinGuildRequest(guildTopic) {
|
||||
const guildTopicBuffer = b4a.from(guildTopic, 'hex');
|
||||
if (!activeRooms.some(room => room.topic === guildTopic)) {
|
||||
try {
|
||||
const swarm = new Hyperswarm();
|
||||
const discovery = swarm.join(guildTopicBuffer, { client: true, server: true });
|
||||
await discovery.flushed();
|
||||
|
||||
swarm.on('connection', (connection, info) => {
|
||||
handleConnection(connection, info);
|
||||
// Request guild information from peers
|
||||
const guildRequestMessage = JSON.stringify({
|
||||
type: 'guildRequest',
|
||||
guildTopic
|
||||
});
|
||||
connection.write(guildRequestMessage);
|
||||
});
|
||||
|
||||
activeRooms.push({ topic: guildTopic, swarm, discovery });
|
||||
|
||||
console.log('Joined guild topic:', guildTopic); // Debugging log
|
||||
|
||||
updatePeerCount();
|
||||
} catch (error) {
|
||||
console.error('Error joining swarm for guild topic:', guildTopic, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
initialize();
|
Loading…
Reference in New Issue
Block a user