From e2991fc1d1aec0f28eb6e1deae49da5e8024019c Mon Sep 17 00:00:00 2001 From: Late Night Defender Date: Mon, 9 Jun 2025 02:36:36 +0700 Subject: [PATCH] check the data type before calling JSON.parse() --- src/App.jsx | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 43793af..26774c8 100644 --- a/src/App.jsx +++ b/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) => { - const msg = JSON.parse(event.data); - if (msg.type === 'transcript') { - setTranscript(msg.text || ''); - } else if (msg.type === 'content') { - setAiReply(prev => prev + (msg.delta || '')); - } else if (msg.type === 'audio') { - const audioBlob = new Blob([msg.audio], { type: 'audio/mpeg' }); + // Check if message is binary (audio), not JSON + if (typeof event.data !== 'string') { + // Handle binary audio message + const audioBlob = new Blob([event.data], { type: 'audio/mpeg' }); const audioUrl = URL.createObjectURL(audioBlob); const audio = new Audio(audioUrl); audio.play().catch(() => { 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); } };