mirror of
https://github.com/xtr-dev/rondevu-demo.git
synced 2025-12-15 12:33:23 +00:00
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:
@@ -87,22 +87,48 @@ async function main() {
|
|||||||
const dc = pc.createDataChannel('chat')
|
const dc = pc.createDataChannel('chat')
|
||||||
|
|
||||||
// Set up data channel handlers
|
// Set up data channel handlers
|
||||||
|
let identified = false
|
||||||
|
|
||||||
dc.onopen = () => {
|
dc.onopen = () => {
|
||||||
console.log(' ✓ Data channel opened!')
|
console.log(' ✓ Data channel opened!')
|
||||||
console.log(`\n📤 Sending message: "${MESSAGE}"`)
|
|
||||||
dc.send(MESSAGE)
|
|
||||||
|
|
||||||
// Close after sending
|
// Send identify message (demo protocol)
|
||||||
|
console.log(`📤 Sending identify message...`)
|
||||||
|
dc.send(JSON.stringify({
|
||||||
|
type: 'identify',
|
||||||
|
from: rondevu.getUsername()
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
dc.onmessage = (event) => {
|
||||||
|
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(() => {
|
setTimeout(() => {
|
||||||
console.log('\n✅ Test completed successfully!')
|
console.log('\n✅ Test completed successfully!')
|
||||||
dc.close()
|
dc.close()
|
||||||
pc.close()
|
pc.close()
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
}, 1000)
|
}, 5000)
|
||||||
|
} else if (msg.type === 'message') {
|
||||||
|
console.log(`💬 @${msg.from || 'peer'}: ${msg.text}`)
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.log(`📥 Received (raw): ${event.data}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
dc.onmessage = (event) => {
|
|
||||||
console.log(`📥 Received: ${event.data}`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dc.onerror = (error) => {
|
dc.onerror = (error) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user