v53: сервер ждёт закрытия от клиента — close frame не обгоняет text frame

This commit is contained in:
“Naeel”
2026-05-22 17:29:10 +04:00
parent 628fea51b7
commit f4957437db
+14 -17
View File
@@ -53,7 +53,7 @@ let mediaRecorder, audioChunks = [], userAudioBlob = null, ttsText = '';
let isRecording = false, isAnalyzing = false, micAnalyser = null, micAnimId = null;
let recordStartTime = 0, recordDuration = 0;
// ---------- Версия ----------
const VERSION = 'v52';
const VERSION = 'v53';
document.getElementById('ver').textContent = VERSION;
// ---------- Перевод на русский (Groq) ----------
@@ -635,46 +635,43 @@ async function transcribe(blob) {
return new Promise((resolve) => {
let settled = false;
const done = (r) => { if (settled) return; settled = true; try { ws.close(); } catch(e){} resolve(r); };
const done = (r) => { if (settled) return; settled = true; resolve(r); };
const payload = JSON.stringify({ token: GROQ_API_KEY, audio_b64: base64, mime: blob.type || 'audio/webm' });
const ws = new WebSocket('wss://proxy.kube5s.ru/ws');
const timeout = setTimeout(() => { console.log('[WS] timeout'); done({ text: '', error: 'timeout' }); }, 60000);
const timeout = setTimeout(() => { console.log('[WS] timeout'); try { ws.close(); } catch(e){} done({ text: '', error: 'timeout' }); }, 15000);
// Полинг readyState — onopen может не сработать если WS открылся до присвоения
const trySend = () => {
if (settled) return;
const st = ws.readyState;
if (st === WebSocket.OPEN) {
try { ws.send(payload); console.log('[WS] sent'); } catch(e) { console.log('[WS] send err:', e); done({ text: '', error: 'network' }); }
try { ws.send(payload); console.log('[WS] sent'); } catch(e) { console.log('[WS] send err:', e); clearTimeout(timeout); done({ text: '', error: 'network' }); }
return;
}
if (st === WebSocket.CONNECTING) { setTimeout(trySend, 20); return; }
// CLOSING or CLOSED
console.log('[WS] aborted, state=' + st);
clearTimeout(timeout);
done({ text: '', error: 'network' });
};
ws.onopen = trySend;
// Если WS уже открыт — onopen не вызовется, trySend сам отработает
trySend();
ws.onmessage = (e) => {
clearTimeout(timeout);
const data = JSON.parse(e.data);
console.log('[WS] ← ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)'));
done(data.error ? { text: '', error: data.error } : { text: data.text || '', error: null });
try { ws.close(); } catch(ex) {}
try {
const data = JSON.parse(e.data);
console.log('[WS] ← ' + ((performance.now()-tStart)/1000).toFixed(2) + 's → ' + (data.text||'(empty)'));
done(data.error ? { text: '', error: data.error } : { text: data.text || '', error: null });
} catch(ex) {
console.log('[WS] parse err:', ex);
done({ text: '', error: 'parse' });
}
};
ws.onerror = () => { console.log('[WS] error'); clearTimeout(timeout); done({ text: '', error: 'network' }); };
ws.onclose = (e) => {
console.log('[WS] close code=' + e.code);
// close может прийти раньше onmessage — даём фору 500ms
setTimeout(() => {
if (!settled) { clearTimeout(timeout); done({ text: '', error: 'network' }); }
}, 500);
};
ws.onclose = (e) => { console.log('[WS] close code=' + e.code); /* не резолвим — клиент сам закрывает после onmessage */ };
});
}