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
This commit is contained in:
2025-12-12 21:26:01 +01:00
parent 778fa2e3a9
commit 9e0728f74a

View File

@@ -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) => {