// Start speaking a long sentence, then abort a few seconds in. // Usage: node abort-demo.mjs const PORT = process.env.TTS_PORT ?? '11500' const text = 'This is a very long sentence that will be cut off before it finishes, ' + 'because a few seconds after playback starts we will send an abort command ' + 'to demonstrate the abort current functionality of the server.' const ws = new WebSocket(`ws://localhost:${PORT}`) ws.addEventListener('open', () => { ws.send(JSON.stringify({ type: 'speak', text })) }) ws.addEventListener('message', ({ data }) => { const msg = JSON.parse(data) console.log(msg) if (msg.event === 'started') { setTimeout(() => { console.log('aborting...') ws.send(JSON.stringify({ type: 'abort_current' })) }, 3000) } if (msg.event === 'aborted' || msg.event === 'finished' || msg.event === 'error') { ws.close() } }) ws.addEventListener('error', (e) => { console.error('error:', e.message) process.exit(1) })