check the data type before calling JSON.parse()
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
2ed956a0f3
commit
e2991fc1d1
1 changed files with 18 additions and 7 deletions
25
src/App.jsx
25
src/App.jsx
|
@ -41,18 +41,29 @@ Only reply with one or two short English sentences at a time.`; // (keep your fu
|
||||||
};
|
};
|
||||||
|
|
||||||
ws.onmessage = (event) => {
|
ws.onmessage = (event) => {
|
||||||
const msg = JSON.parse(event.data);
|
// Check if message is binary (audio), not JSON
|
||||||
if (msg.type === 'transcript') {
|
if (typeof event.data !== 'string') {
|
||||||
setTranscript(msg.text || '');
|
// Handle binary audio message
|
||||||
} else if (msg.type === 'content') {
|
const audioBlob = new Blob([event.data], { type: 'audio/mpeg' });
|
||||||
setAiReply(prev => prev + (msg.delta || ''));
|
|
||||||
} else if (msg.type === 'audio') {
|
|
||||||
const audioBlob = new Blob([msg.audio], { type: 'audio/mpeg' });
|
|
||||||
const audioUrl = URL.createObjectURL(audioBlob);
|
const audioUrl = URL.createObjectURL(audioBlob);
|
||||||
const audio = new Audio(audioUrl);
|
const audio = new Audio(audioUrl);
|
||||||
audio.play().catch(() => {
|
audio.play().catch(() => {
|
||||||
console.warn("Mobile autoplay may be blocked until user gesture");
|
console.warn("Mobile autoplay may be blocked until user gesture");
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse JSON message (transcript, content, etc.)
|
||||||
|
try {
|
||||||
|
const msg = JSON.parse(event.data);
|
||||||
|
|
||||||
|
if (msg.type === 'transcript') {
|
||||||
|
setTranscript(msg.text || '');
|
||||||
|
} else if (msg.type === 'content') {
|
||||||
|
setAiReply(prev => prev + (msg.delta || ''));
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Failed to parse JSON", event.data, err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue