vish85521 commited on
Commit
71064ed
·
verified ·
1 Parent(s): 1f5ac5b

Update public/script.js

Browse files
Files changed (1) hide show
  1. public/script.js +29 -15
public/script.js CHANGED
@@ -9,14 +9,12 @@ const audioContainer = document.getElementById('audio-container');
9
  let localStream;
10
  const peers = {}; // Store connections to other users
11
 
12
- // WebRTC Configuration using free Google STUN servers
13
- // WebRTC Configuration using Google STUN and a free public TURN server
14
  const rtcConfig = {
15
  iceServers: [
16
- // STUN helps find your public IP
17
  { urls: 'stun:stun.l.google.com:19302' },
18
-
19
- // TURN relays the audio when firewalls block direct connections
20
  {
21
  urls: 'turn:openrelay.metered.ca:80',
22
  username: 'openrelayproject',
@@ -80,7 +78,7 @@ joinBtn.onclick = async () => {
80
  socket.on('user-connected', (userId) => {
81
  if (!localStream) return; // Don't call if we aren't in the audio room
82
 
83
- appendSystemMessage(`User ${userId.substring(0,4)} joined.`);
84
  const peer = createPeerConnection(userId, true);
85
  peers[userId] = peer;
86
  });
@@ -98,15 +96,19 @@ socket.on('signal', async (data) => {
98
 
99
  const peer = peers[from];
100
 
101
- if (signal.type === 'offer') {
102
- await peer.setRemoteDescription(new RTCSessionDescription(signal));
103
- const answer = await peer.createAnswer();
104
- await peer.setLocalDescription(answer);
105
- socket.emit('signal', { to: from, signal: peer.localDescription });
106
- } else if (signal.type === 'answer') {
107
- await peer.setRemoteDescription(new RTCSessionDescription(signal));
108
- } else if (signal.candidate) {
109
- await peer.addIceCandidate(new RTCIceCandidate(signal));
 
 
 
 
110
  }
111
  });
112
 
@@ -138,6 +140,18 @@ function createPeerConnection(targetUserId, isInitiator) {
138
  }
139
  };
140
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  // When we receive their audio stream, play it
142
  peer.ontrack = (event) => {
143
  let audioEl = document.getElementById(`audio-${targetUserId}`);
 
9
  let localStream;
10
  const peers = {}; // Store connections to other users
11
 
12
+ // --- WebRTC Configuration ---
13
+ // Includes Google STUN servers and the Metered.ca public TURN fallback
14
  const rtcConfig = {
15
  iceServers: [
 
16
  { urls: 'stun:stun.l.google.com:19302' },
17
+ { urls: 'stun:stun1.l.google.com:19302' }, // Backup STUN
 
18
  {
19
  urls: 'turn:openrelay.metered.ca:80',
20
  username: 'openrelayproject',
 
78
  socket.on('user-connected', (userId) => {
79
  if (!localStream) return; // Don't call if we aren't in the audio room
80
 
81
+ appendSystemMessage(`User ${userId.substring(0,4)} joined. Attempting connection...`);
82
  const peer = createPeerConnection(userId, true);
83
  peers[userId] = peer;
84
  });
 
96
 
97
  const peer = peers[from];
98
 
99
+ try {
100
+ if (signal.type === 'offer') {
101
+ await peer.setRemoteDescription(new RTCSessionDescription(signal));
102
+ const answer = await peer.createAnswer();
103
+ await peer.setLocalDescription(answer);
104
+ socket.emit('signal', { to: from, signal: peer.localDescription });
105
+ } else if (signal.type === 'answer') {
106
+ await peer.setRemoteDescription(new RTCSessionDescription(signal));
107
+ } else if (signal.candidate) {
108
+ await peer.addIceCandidate(new RTCIceCandidate(signal));
109
+ }
110
+ } catch (err) {
111
+ console.error("Signaling error:", err);
112
  }
113
  });
114
 
 
140
  }
141
  };
142
 
143
+ // TRACK THE CONNECTION STATUS (Crucial for debugging NAT/Firewall issues)
144
+ peer.oniceconnectionstatechange = () => {
145
+ console.log(`Connection state with ${targetUserId}:`, peer.iceConnectionState);
146
+ if (peer.iceConnectionState === 'failed') {
147
+ appendSystemMessage(`❌ Connection to ${targetUserId.substring(0,4)} failed. Firewalls blocked it.`);
148
+ } else if (peer.iceConnectionState === 'connected') {
149
+ appendSystemMessage(`✅ Successfully connected to ${targetUserId.substring(0,4)}! Audio flowing.`);
150
+ } else if (peer.iceConnectionState === 'disconnected') {
151
+ appendSystemMessage(`⚠️ Connection to ${targetUserId.substring(0,4)} lost. Trying to reconnect...`);
152
+ }
153
+ };
154
+
155
  // When we receive their audio stream, play it
156
  peer.ontrack = (event) => {
157
  let audioEl = document.getElementById(`audio-${targetUserId}`);