#!/usr/bin/env node /** * Test script to connect to @bas's chat service and send a "hello" message * * IMPORTANT: This script requires the 'wrtc' package which must be installed separately. * See TEST_README.md for detailed installation instructions. * * Quick start: * npm install wrtc * npm test * * Requirements: * - Node.js 19+ (or Node.js 18 with --experimental-global-webcrypto) * - wrtc package (requires native compilation) * - Build tools (python, make, g++) */ import { Rondevu, NodeCryptoAdapter } from '@xtr-dev/rondevu-client' // Import wrtc let wrtc try { const wrtcModule = await import('wrtc') wrtc = wrtcModule.default || wrtcModule } catch (error) { console.error('āŒ Error: wrtc package not found or failed to load') console.error('\nThe wrtc package is required for WebRTC support in Node.js.') console.error('Install it with:') console.error('\n npm install wrtc') console.error('\nNote: wrtc requires native compilation and may take a few minutes to install.') console.error('\nError details:', error.message) process.exit(1) } const { RTCPeerConnection } = wrtc // Configuration const API_URL = 'https://api.ronde.vu' const TARGET_USER = 'bas' const SERVICE_FQN = `chat:2.0.0@${TARGET_USER}` const MESSAGE = 'hello' async function main() { console.log('šŸš€ Rondevu Test Script') console.log('='.repeat(50)) try { // 1. Connect to Rondevu with Node crypto adapter and ICE preset console.log('1. Connecting to Rondevu...') const rondevu = await Rondevu.connect({ apiUrl: API_URL, username: `test-${Date.now()}`, // Anonymous test user cryptoAdapter: new NodeCryptoAdapter(), iceServers: 'ipv4-turn' // Use ICE server preset }) console.log(` āœ“ Connected as: ${rondevu.getUsername()}`) console.log(` āœ“ Public key: ${rondevu.getPublicKey()?.substring(0, 20)}...`) // 2. Connect to service (automatic setup) console.log(`\n2. Connecting to service: ${SERVICE_FQN}`) let identified = false const connection = await rondevu.connectToService({ serviceFqn: SERVICE_FQN, onConnection: ({ dc, peerUsername }) => { console.log(`āœ… Connected to @${peerUsername}`) // Set up message handler dc.addEventListener('message', (event) => { console.log(`šŸ“„ RAW DATA:`, event.data) try { const msg = JSON.parse(event.data) console.log(`šŸ“„ Parsed message:`, JSON.stringify(msg, null, 2)) 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!') connection.dc.close() connection.pc.close() process.exit(0) }, 5000) } else if (msg.type === 'message') { console.log(`šŸ’¬ @${msg.from || 'peer'}: ${msg.text}`) } else { console.log(`šŸ“„ Unknown message type: ${msg.type}`) } } catch (err) { console.log(`šŸ“„ Parse error:`, err.message) console.log(`šŸ“„ Raw data was:`, event.data) } }) // Send identify message after channel opens console.log(`šŸ“¤ Sending identify message...`) const identifyMsg = JSON.stringify({ type: 'identify', from: rondevu.getUsername() }) dc.send(identifyMsg) console.log(` āœ“ Identify message sent`) } }) // Monitor connection state connection.pc.onconnectionstatechange = () => { console.log(` Connection state: ${connection.pc.connectionState}`) if (connection.pc.connectionState === 'failed') { console.error('āŒ Connection failed') process.exit(1) } } connection.pc.oniceconnectionstatechange = () => { console.log(` ICE state: ${connection.pc.iceConnectionState}`) } console.log('\nā³ Waiting for messages...') // Timeout after 30 seconds setTimeout(() => { if (connection.pc.connectionState !== 'connected') { console.error('āŒ Connection timeout') process.exit(1) } }, 30000) } catch (error) { console.error('\nāŒ Error:', error.message) console.error(error) process.exit(1) } } main()