From 9e0728f74a5d7210a0e6505f591f6c25fb32faea Mon Sep 17 00:00:00 2001 From: Bas van den Aakster Date: Fri, 12 Dec 2025 21:26:01 +0100 Subject: [PATCH] Implement demo message protocol in test script - Send 'identify' message on connection - Wait for 'identify_ack' acknowledgment - Send 'message' type with text for chat - Keep connection open 5s to receive responses --- test-connect.js | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/test-connect.js b/test-connect.js index 0af4a36..2252b50 100644 --- a/test-connect.js +++ b/test-connect.js @@ -87,22 +87,48 @@ async function main() { const dc = pc.createDataChannel('chat') // Set up data channel handlers + let identified = false + dc.onopen = () => { console.log(' āœ“ Data channel opened!') - console.log(`\nšŸ“¤ Sending message: "${MESSAGE}"`) - dc.send(MESSAGE) - // Close after sending - setTimeout(() => { - console.log('\nāœ… Test completed successfully!') - dc.close() - pc.close() - process.exit(0) - }, 1000) + // Send identify message (demo protocol) + console.log(`šŸ“¤ Sending identify message...`) + dc.send(JSON.stringify({ + type: 'identify', + from: rondevu.getUsername() + })) } dc.onmessage = (event) => { - console.log(`šŸ“„ Received: ${event.data}`) + try { + const msg = JSON.parse(event.data) + console.log(`šŸ“„ Received message:`, msg) + + if (msg.type === 'identify_ack' && !identified) { + identified = true + console.log(`āœ… Connection acknowledged by @${msg.from}`) + + // Now send the actual chat message + console.log(`šŸ“¤ Sending chat message: "${MESSAGE}"`) + dc.send(JSON.stringify({ + type: 'message', + text: MESSAGE + })) + + // Keep connection open longer to see if we get a response + setTimeout(() => { + console.log('\nāœ… Test completed successfully!') + dc.close() + pc.close() + process.exit(0) + }, 5000) + } else if (msg.type === 'message') { + console.log(`šŸ’¬ @${msg.from || 'peer'}: ${msg.text}`) + } + } catch (err) { + console.log(`šŸ“„ Received (raw): ${event.data}`) + } } dc.onerror = (error) => {