if websocket connection fails, frontend does not allow recording

This commit is contained in:
Quentin Fuxa 2024-12-31 11:17:41 +01:00
parent 58e48bb717
commit b7a2d23a18

View file

@ -101,11 +101,11 @@
<div id="transcriptions"></div> <div id="transcriptions"></div>
<script> <script>
let isRecording = false, let isRecording = false;
websocket, let websocket = null;
recorder, let recorder = null;
chunkDuration = 1000, let chunkDuration = 1000;
websocketUrl = "ws://localhost:8000/asr"; let websocketUrl = "ws://localhost:8000/asr";
// Tracks whether the user voluntarily closed the WebSocket // Tracks whether the user voluntarily closed the WebSocket
let userClosing = false; let userClosing = false;
@ -137,46 +137,56 @@
statusText.textContent = "WebSocket URL updated. Ready to connect."; statusText.textContent = "WebSocket URL updated. Ready to connect.";
}); });
/**
* Opens webSocket connection.
* returns a Promise that resolves when the connection is open.
* rejects if there was an error.
*/
function setupWebSocket() { function setupWebSocket() {
try { return new Promise((resolve, reject) => {
websocket = new WebSocket(websocketUrl); try {
} catch (error) { websocket = new WebSocket(websocketUrl);
statusText.textContent = } catch (error) {
"Invalid WebSocket URL. Please check the URL and try again."; statusText.textContent =
throw error; "Invalid WebSocket URL. Please check the URL and try again.";
} reject(error);
return;
websocket.onopen = () => {
statusText.textContent = "Connected to server";
};
websocket.onclose = (event) => {
if (userClosing) {
statusText.textContent = "WebSocket closed by user.";
} else {
statusText.textContent = "Disconnected from server (unexpected).";
} }
userClosing = false;
};
websocket.onerror = () => { websocket.onopen = () => {
statusText.textContent = "Error connecting to WebSocket"; statusText.textContent = "Connected to server";
stopRecording(); resolve();
}; };
websocket.onmessage = (event) => { websocket.onclose = (event) => {
const data = JSON.parse(event.data); // If we manually closed it, we say so
const { transcription, buffer } = data; if (userClosing) {
statusText.textContent = "WebSocket closed by user.";
} else {
statusText.textContent = "Disconnected from the websocket server. If this is the first launch, the model may be downloading in the backend. Check the API logs for more information.";
}
userClosing = false;
};
// Update confirmed transcription websocket.onerror = () => {
fullTranscription += transcription; statusText.textContent = "Error connecting to WebSocket";
reject(new Error("Error connecting to WebSocket"));
};
// Update the transcription display websocket.onmessage = (event) => {
transcriptionsDiv.innerHTML = ` const data = JSON.parse(event.data);
<span class="transcription">${fullTranscription}</span> const { transcription, buffer } = data;
<span class="buffer">${buffer}</span>
`; // Update confirmed transcription
}; fullTranscription += transcription;
// Update the transcription display
transcriptionsDiv.innerHTML = `
<span class="transcription">${fullTranscription}</span>
<span class="buffer">${buffer}</span>
`;
};
});
} }
async function startRecording() { async function startRecording() {
@ -200,12 +210,18 @@
function stopRecording() { function stopRecording() {
userClosing = true; userClosing = true;
recorder?.stop(); // Stop the recorder if it exists
recorder = null; if (recorder) {
recorder.stop();
recorder = null;
}
isRecording = false; isRecording = false;
websocket?.close(); // Close the websocket if it exists
websocket = null; if (websocket) {
websocket.close();
websocket = null;
}
updateUI(); updateUI();
} }
@ -216,12 +232,12 @@
transcriptionsDiv.innerHTML = ""; transcriptionsDiv.innerHTML = "";
try { try {
setupWebSocket(); await setupWebSocket();
await startRecording();
} catch (err) { } catch (err) {
return; statusText.textContent =
"Could not connect to WebSocket or access mic. Recording aborted.";
} }
await startRecording();
} else { } else {
stopRecording(); stopRecording();
} }