qfuxa commited on
Commit
b7a2d23
·
1 Parent(s): 58e48bb

if websocket connection fails, frontend does not allow recording

Browse files
Files changed (1) hide show
  1. src/live_transcription.html +62 -46
src/live_transcription.html CHANGED
@@ -101,11 +101,11 @@
101
  <div id="transcriptions"></div>
102
 
103
  <script>
104
- let isRecording = false,
105
- websocket,
106
- recorder,
107
- chunkDuration = 1000,
108
- websocketUrl = "ws://localhost:8000/asr";
109
 
110
  // Tracks whether the user voluntarily closed the WebSocket
111
  let userClosing = false;
@@ -137,46 +137,56 @@
137
  statusText.textContent = "WebSocket URL updated. Ready to connect.";
138
  });
139
 
 
 
 
 
 
140
  function setupWebSocket() {
141
- try {
142
- websocket = new WebSocket(websocketUrl);
143
- } catch (error) {
144
- statusText.textContent =
145
- "Invalid WebSocket URL. Please check the URL and try again.";
146
- throw error;
147
- }
 
 
148
 
149
- websocket.onopen = () => {
150
- statusText.textContent = "Connected to server";
151
- };
 
152
 
153
- websocket.onclose = (event) => {
154
- if (userClosing) {
155
- statusText.textContent = "WebSocket closed by user.";
156
- } else {
157
- statusText.textContent = "Disconnected from server (unexpected).";
158
- }
159
- userClosing = false;
160
- };
 
161
 
162
- websocket.onerror = () => {
163
- statusText.textContent = "Error connecting to WebSocket";
164
- stopRecording();
165
- };
166
 
167
- websocket.onmessage = (event) => {
168
- const data = JSON.parse(event.data);
169
- const { transcription, buffer } = data;
170
 
171
- // Update confirmed transcription
172
- fullTranscription += transcription;
173
 
174
- // Update the transcription display
175
- transcriptionsDiv.innerHTML = `
176
- <span class="transcription">${fullTranscription}</span>
177
- <span class="buffer">${buffer}</span>
178
- `;
179
- };
 
180
  }
181
 
182
  async function startRecording() {
@@ -200,12 +210,18 @@
200
  function stopRecording() {
201
  userClosing = true;
202
 
203
- recorder?.stop();
204
- recorder = null;
 
 
 
205
  isRecording = false;
206
 
207
- websocket?.close();
208
- websocket = null;
 
 
 
209
 
210
  updateUI();
211
  }
@@ -216,12 +232,12 @@
216
  transcriptionsDiv.innerHTML = "";
217
 
218
  try {
219
- setupWebSocket();
 
220
  } catch (err) {
221
- return;
 
222
  }
223
-
224
- await startRecording();
225
  } else {
226
  stopRecording();
227
  }
 
101
  <div id="transcriptions"></div>
102
 
103
  <script>
104
+ let isRecording = false;
105
+ let websocket = null;
106
+ let recorder = null;
107
+ let chunkDuration = 1000;
108
+ let websocketUrl = "ws://localhost:8000/asr";
109
 
110
  // Tracks whether the user voluntarily closed the WebSocket
111
  let userClosing = false;
 
137
  statusText.textContent = "WebSocket URL updated. Ready to connect.";
138
  });
139
 
140
+ /**
141
+ * Opens webSocket connection.
142
+ * returns a Promise that resolves when the connection is open.
143
+ * rejects if there was an error.
144
+ */
145
  function setupWebSocket() {
146
+ return new Promise((resolve, reject) => {
147
+ try {
148
+ websocket = new WebSocket(websocketUrl);
149
+ } catch (error) {
150
+ statusText.textContent =
151
+ "Invalid WebSocket URL. Please check the URL and try again.";
152
+ reject(error);
153
+ return;
154
+ }
155
 
156
+ websocket.onopen = () => {
157
+ statusText.textContent = "Connected to server";
158
+ resolve();
159
+ };
160
 
161
+ websocket.onclose = (event) => {
162
+ // If we manually closed it, we say so
163
+ if (userClosing) {
164
+ statusText.textContent = "WebSocket closed by user.";
165
+ } else {
166
+ 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.";
167
+ }
168
+ userClosing = false;
169
+ };
170
 
171
+ websocket.onerror = () => {
172
+ statusText.textContent = "Error connecting to WebSocket";
173
+ reject(new Error("Error connecting to WebSocket"));
174
+ };
175
 
176
+ websocket.onmessage = (event) => {
177
+ const data = JSON.parse(event.data);
178
+ const { transcription, buffer } = data;
179
 
180
+ // Update confirmed transcription
181
+ fullTranscription += transcription;
182
 
183
+ // Update the transcription display
184
+ transcriptionsDiv.innerHTML = `
185
+ <span class="transcription">${fullTranscription}</span>
186
+ <span class="buffer">${buffer}</span>
187
+ `;
188
+ };
189
+ });
190
  }
191
 
192
  async function startRecording() {
 
210
  function stopRecording() {
211
  userClosing = true;
212
 
213
+ // Stop the recorder if it exists
214
+ if (recorder) {
215
+ recorder.stop();
216
+ recorder = null;
217
+ }
218
  isRecording = false;
219
 
220
+ // Close the websocket if it exists
221
+ if (websocket) {
222
+ websocket.close();
223
+ websocket = null;
224
+ }
225
 
226
  updateUI();
227
  }
 
232
  transcriptionsDiv.innerHTML = "";
233
 
234
  try {
235
+ await setupWebSocket();
236
+ await startRecording();
237
  } catch (err) {
238
+ statusText.textContent =
239
+ "Could not connect to WebSocket or access mic. Recording aborted.";
240
  }
 
 
241
  } else {
242
  stopRecording();
243
  }