seawolf2357 commited on
Commit
228f8d9
ยท
verified ยท
1 Parent(s): 21de233

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +68 -14
index.html CHANGED
@@ -439,9 +439,14 @@
439
  }, 5000);
440
  }
441
 
442
- async function setupWebRTC() {
443
  const config = __RTC_CONFIGURATION__;
444
  peerConnection = new RTCPeerConnection(config);
 
 
 
 
 
445
  const timeoutId = setTimeout(() => {
446
  const toast = document.getElementById('error-toast');
447
  toast.textContent = "์—ฐ๊ฒฐ์ด ํ‰์†Œ๋ณด๋‹ค ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. VPN์„ ์‚ฌ์šฉ ์ค‘์ด์‹ ๊ฐ€์š”?";
@@ -495,6 +500,27 @@
495
  clearTimeout(timeoutId);
496
  const toast = document.getElementById('error-toast');
497
  toast.style.display = 'none';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
498
  }
499
  updateButtonState();
500
  });
@@ -517,11 +543,25 @@
517
  return;
518
  }
519
  await peerConnection.setRemoteDescription(serverResponse);
 
 
 
 
 
520
  const eventSource = new EventSource('/outputs?webrtc_id=' + webrtc_id);
 
 
521
  eventSource.addEventListener("output", (event) => {
522
  const eventJson = JSON.parse(event.data);
523
  addMessage("assistant", eventJson.content);
524
  });
 
 
 
 
 
 
 
525
  } catch (err) {
526
  clearTimeout(timeoutId);
527
  console.error('Error setting up WebRTC:', err);
@@ -549,21 +589,35 @@
549
  audioSource = null;
550
  }
551
  if (peerConnection) {
552
- if (peerConnection.getTransceivers) {
553
- peerConnection.getTransceivers().forEach(transceiver => {
554
- if (transceiver.stop) {
555
- transceiver.stop();
556
- }
557
- });
558
- }
559
- if (peerConnection.getSenders) {
560
- peerConnection.getSenders().forEach(sender => {
561
- if (sender.track && sender.track.stop) sender.track.stop();
562
- });
 
 
 
 
 
 
563
  }
564
- console.log('closing');
565
- peerConnection.close();
 
 
 
 
 
 
 
566
  }
 
567
  updateButtonState();
568
  audioLevel = 0;
569
  }
 
439
  }, 5000);
440
  }
441
 
442
+ async function setupWebRTC() {
443
  const config = __RTC_CONFIGURATION__;
444
  peerConnection = new RTCPeerConnection(config);
445
+
446
+ // ์—ฐ๊ฒฐ ์žฌ์‹œ๋„ ๋ฉ”์ปค๋‹ˆ์ฆ˜ ์ถ”๊ฐ€
447
+ let retryCount = 0;
448
+ const maxRetries = 3;
449
+
450
  const timeoutId = setTimeout(() => {
451
  const toast = document.getElementById('error-toast');
452
  toast.textContent = "์—ฐ๊ฒฐ์ด ํ‰์†Œ๋ณด๋‹ค ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. VPN์„ ์‚ฌ์šฉ ์ค‘์ด์‹ ๊ฐ€์š”?";
 
500
  clearTimeout(timeoutId);
501
  const toast = document.getElementById('error-toast');
502
  toast.style.display = 'none';
503
+ retryCount = 0; // ์—ฐ๊ฒฐ ์„ฑ๊ณต ์‹œ ์žฌ์‹œ๋„ ์นด์šดํŠธ ์ดˆ๊ธฐํ™”
504
+ } else if (peerConnection.connectionState === 'failed' || peerConnection.connectionState === 'disconnected') {
505
+ if (retryCount < maxRetries) {
506
+ retryCount++;
507
+ console.log(`์—ฐ๊ฒฐ ์‹คํŒจ, ์žฌ์‹œ๋„ ์ค‘... (${retryCount}/${maxRetries})`);
508
+
509
+ // ํ˜„์žฌ ์—ฐ๊ฒฐ ์ •๋ฆฌ
510
+ if (peerConnection) {
511
+ peerConnection.close();
512
+ }
513
+
514
+ // ์ž ์‹œ ํ›„ ์žฌ์—ฐ๊ฒฐ ์‹œ๋„
515
+ setTimeout(() => {
516
+ if (peerConnection.connectionState !== 'connected') {
517
+ showError(`์—ฐ๊ฒฐ ์žฌ์‹œ๋„ ์ค‘... (${retryCount}/${maxRetries})`);
518
+ setupWebRTC();
519
+ }
520
+ }, 2000); // 2์ดˆ ํ›„ ์žฌ์‹œ๋„
521
+ } else {
522
+ showError('์—ฐ๊ฒฐ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ƒˆ๋กœ๊ณ ์นจ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”.');
523
+ }
524
  }
525
  updateButtonState();
526
  });
 
543
  return;
544
  }
545
  await peerConnection.setRemoteDescription(serverResponse);
546
+ // ์ด์ „ EventSource๊ฐ€ ์žˆ์œผ๋ฉด ๋‹ซ๊ธฐ
547
+ if (window.currentEventSource) {
548
+ window.currentEventSource.close();
549
+ }
550
+
551
  const eventSource = new EventSource('/outputs?webrtc_id=' + webrtc_id);
552
+ window.currentEventSource = eventSource; // ์ „์—ญ์œผ๋กœ ์ €์žฅํ•˜์—ฌ ๋‚˜์ค‘์— ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ
553
+
554
  eventSource.addEventListener("output", (event) => {
555
  const eventJson = JSON.parse(event.data);
556
  addMessage("assistant", eventJson.content);
557
  });
558
+
559
+ eventSource.addEventListener("error", (event) => {
560
+ console.error("EventSource ์˜ค๋ฅ˜:", event);
561
+ if (event.target.readyState === EventSource.CLOSED) {
562
+ console.log("EventSource ์—ฐ๊ฒฐ์ด ๋‹ซํ˜”์Šต๋‹ˆ๋‹ค.");
563
+ }
564
+ });
565
  } catch (err) {
566
  clearTimeout(timeoutId);
567
  console.error('Error setting up WebRTC:', err);
 
589
  audioSource = null;
590
  }
591
  if (peerConnection) {
592
+ try {
593
+ if (peerConnection.getTransceivers) {
594
+ peerConnection.getTransceivers().forEach(transceiver => {
595
+ if (transceiver.stop) {
596
+ transceiver.stop();
597
+ }
598
+ });
599
+ }
600
+ if (peerConnection.getSenders) {
601
+ peerConnection.getSenders().forEach(sender => {
602
+ if (sender.track && sender.track.stop) sender.track.stop();
603
+ });
604
+ }
605
+ console.log('closing connection');
606
+ peerConnection.close();
607
+ } catch (error) {
608
+ console.error('Error during connection cleanup:', error);
609
  }
610
+
611
+ // ํด๋ฆฐ์—… ์ง„ํ–‰ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋”๋ผ๋„ null๋กœ ์„ค์ •ํ•˜์—ฌ ์ƒˆ ์—ฐ๊ฒฐ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•จ
612
+ peerConnection = null;
613
+ }
614
+
615
+ // EventSource ๋‹ซ๊ธฐ (์žˆ๋Š” ๊ฒฝ์šฐ)
616
+ if (window.currentEventSource) {
617
+ window.currentEventSource.close();
618
+ window.currentEventSource = null;
619
  }
620
+
621
  updateButtonState();
622
  audioLevel = 0;
623
  }