Files
tts-server/examples/abort-demo.mjs
mikael-lovqvists-claude-agent f4ae96c6b9 Replace HTTP API with WebSocket server
Single port (TTS_PORT) handles both the WS upgrade handshake and
connections. Adds job queue, generation worker, playback events
(queued/started/finished/aborted/error), and abort_current/abort_all
commands. Fixes BrokenPipeError when pacat is killed mid-write.
Updates all examples to use WebSocket; adds abort-demo.mjs.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-09 21:32:58 +00:00

35 lines
987 B
JavaScript

// 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)
})