From 181d011b8d41ab8e1e50bd07ece22032300c3d7c Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Sun, 7 Jul 2024 21:08:30 -0400 Subject: [PATCH] more test --- app.js | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index d795b4d..64a846c 100644 --- a/app.js +++ b/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) { } } -initialize(); \ No newline at end of file +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();