throw errors if websocket connection fails

This commit is contained in:
Quentin Fuxa 2024-12-24 19:31:08 +01:00
parent 0553b75415
commit 50bbd26517

View file

@ -1,8 +1,8 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Audio Transcription</title>
<style>
body {
@ -54,7 +54,8 @@
align-items: flex-start;
gap: 5px;
}
#chunkSelector, #websocketInput {
#chunkSelector,
#websocketInput {
font-size: 16px;
padding: 5px;
border-radius: 5px;
@ -64,7 +65,8 @@
#websocketInput {
width: 200px;
}
#chunkSelector:focus, #websocketInput:focus {
#chunkSelector:focus,
#websocketInput:focus {
outline: none;
border-color: #007bff;
}
@ -99,7 +101,14 @@
<div id="transcriptions"></div>
<script>
let isRecording = false, websocket, recorder, chunkDuration = 1000, websocketUrl = "ws://localhost:8000/ws";
let isRecording = false,
websocket,
recorder,
chunkDuration = 1000,
websocketUrl = "ws://localhost:8000/ws";
// Tracks whether the user voluntarily closed the WebSocket
let userClosing = false;
const statusText = document.getElementById("status");
const recordButton = document.getElementById("recordButton");
@ -114,13 +123,47 @@
chunkDuration = parseInt(chunkSelector.value);
});
// Update WebSocket URL dynamically
// Update WebSocket URL dynamically, with some basic checks
websocketInput.addEventListener("change", () => {
websocketUrl = websocketInput.value;
const urlValue = websocketInput.value.trim();
// Quick check to see if it starts with ws:// or wss://
if (!urlValue.startsWith("ws://") && !urlValue.startsWith("wss://")) {
statusText.textContent =
"Invalid WebSocket URL. It should start with ws:// or wss://";
return;
}
websocketUrl = urlValue;
statusText.textContent = "WebSocket URL updated. Ready to connect.";
});
function setupWebSocket() {
try {
websocket = new WebSocket(websocketUrl);
} catch (error) {
statusText.textContent =
"Invalid WebSocket URL. Please check the URL and try again.";
throw error;
}
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 = () => {
statusText.textContent = "Error connecting to WebSocket";
stopRecording();
};
websocket.onmessage = (event) => {
const data = JSON.parse(event.data);
const { transcription, buffer } = data;
@ -134,42 +177,61 @@
<span class="buffer">${buffer}</span>
`;
};
websocket.onerror = () => {
statusText.textContent = "Error connecting to WebSocket";
stopRecording(); // Stop recording if WebSocket fails
};
}
async function startRecording() {
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
recorder = new MediaRecorder(stream, { mimeType: "audio/webm" });
recorder.ondataavailable = (e) => websocket?.send(e.data);
recorder.start(chunkDuration); // Use dynamic chunk duration
recorder.ondataavailable = (e) => {
if (websocket && websocket.readyState === WebSocket.OPEN) {
websocket.send(e.data);
}
};
recorder.start(chunkDuration);
isRecording = true;
updateUI();
} catch (err) {
statusText.textContent =
"Error accessing microphone. Please allow microphone access.";
}
}
function stopRecording() {
userClosing = true;
recorder?.stop();
recorder = null;
isRecording = false;
websocket?.close();
websocket = null;
updateUI();
}
async function toggleRecording() {
if (isRecording) stopRecording();
else {
if (!isRecording) {
fullTranscription = "";
transcriptionsDiv.innerHTML = "";
try {
setupWebSocket();
} catch (err) {
return;
}
await startRecording();
} else {
stopRecording();
}
}
function updateUI() {
recordButton.classList.toggle("recording", isRecording);
statusText.textContent = isRecording ? "Recording..." : "Click to start transcription";
statusText.textContent = isRecording
? "Recording..."
: "Click to start transcription";
}
recordButton.addEventListener("click", toggleRecording);