Sofia Casadei commited on
Commit
a0a0448
·
1 Parent(s): e523c05

fix: use hf-cloudflare turn server

Browse files
Files changed (1) hide show
  1. static/client.js +17 -20
static/client.js CHANGED
@@ -200,6 +200,22 @@ document.addEventListener('DOMContentLoaded', function() {
200
  dataChannel.onmessage = handleMessage; // Set message handler
201
  console.log('Created data channel');
202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  // Create connection offer
204
  console.log('Creating connection offer...');
205
  const offer = await peerConnection.createOffer();
@@ -207,30 +223,11 @@ document.addEventListener('DOMContentLoaded', function() {
207
  await peerConnection.setLocalDescription(offer);
208
  console.log('Local description set');
209
 
210
- // Wait for ICE gathering to complete (finding connection methods)
211
- console.log('Waiting for ICE gathering...');
212
- await new Promise((resolve) => {
213
- if (peerConnection.iceGatheringState === "complete") {
214
- resolve(); // Already complete
215
- } else {
216
- // Function to check ICE gathering state
217
- const checkState = () => {
218
- if (peerConnection.iceGatheringState === "complete") {
219
- peerConnection.removeEventListener("icegatheringstatechange", checkState);
220
- resolve(); // Complete gathering
221
- }
222
- };
223
- // Listen for ICE gathering state changes
224
- peerConnection.addEventListener("icegatheringstatechange", checkState);
225
- }
226
- });
227
- console.log('ICE gathering complete');
228
-
229
  // Generate random ID for this connection
230
  webrtc_id = Math.random().toString(36).substring(7);
231
  console.log('Generated webrtc_id:', webrtc_id);
232
 
233
- // Send connection offer to server
234
  console.log('Sending offer to server...');
235
  const response = await fetch('/webrtc/offer', {
236
  method: 'POST',
 
200
  dataChannel.onmessage = handleMessage; // Set message handler
201
  console.log('Created data channel');
202
 
203
+ // Add ICE candidate handler to send candidates as they're discovered
204
+ peerConnection.onicecandidate = ({ candidate }) => {
205
+ if (candidate) {
206
+ console.log("Sending ICE candidate", candidate);
207
+ fetch('/webrtc/offer', {
208
+ method: 'POST',
209
+ headers: { 'Content-Type': 'application/json' },
210
+ body: JSON.stringify({
211
+ candidate: candidate.toJSON(),
212
+ webrtc_id: webrtc_id,
213
+ type: "ice-candidate",
214
+ })
215
+ });
216
+ }
217
+ };
218
+
219
  // Create connection offer
220
  console.log('Creating connection offer...');
221
  const offer = await peerConnection.createOffer();
 
223
  await peerConnection.setLocalDescription(offer);
224
  console.log('Local description set');
225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
  // Generate random ID for this connection
227
  webrtc_id = Math.random().toString(36).substring(7);
228
  console.log('Generated webrtc_id:', webrtc_id);
229
 
230
+ // Send connection offer to server immediately
231
  console.log('Sending offer to server...');
232
  const response = await fetch('/webrtc/offer', {
233
  method: 'POST',