Skip to content

Commit 422b8aa

Browse files
author
HoriaBanica
committed
Fixes NAT traversal issues with webRTC by using a TURN server I have set up
1 parent d9951fa commit 422b8aa

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/main/resources/static/sender.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const SEND_FILE_REQUEST = "sendFileRequest";
1717
const SEND_FILE_RESPONSE = "sendFileResponse";
1818
const FILE_RECEIVED = "fileReceived";
1919
const ICE_CANDIDATE = "iceCandidate";
20+
const TURN_PASSWORD = "";
2021
let USERNAME = getUuid();
2122
let PASSWORD = getUuid();
2223
let DIR_HANDLE = null;
@@ -43,15 +44,31 @@ const POLL_INTERVAL = 1000; // Check every second
4344

4445
const STUN_SERVERS = {
4546
iceServers: [
46-
{
47-
urls: ['stun:stun.l.google.com:19302'],
48-
},
49-
{
50-
urls: ['stun:stun.l.google.com:5349'],
51-
}
47+
{
48+
urls: [
49+
'stun:turn.secure-file-share.dedyn.io:3478',
50+
'stuns:turn.secure-file-share.dedyn.io:5349',
51+
'stun:stun.l.google.com:19302',
52+
],
53+
},
54+
{
55+
urls: ['turn:turn.secure-file-share.dedyn.io:3478?transport=udp'],
56+
username: 'turnuser',
57+
credential: TURN_PASSWORD,
58+
},
59+
{
60+
urls: ['turn:turn.secure-file-share.dedyn.io:3478?transport=tcp'],
61+
username: 'turnuser',
62+
credential: TURN_PASSWORD,
63+
},
64+
{
65+
urls: ['turns:turn.secure-file-share.dedyn.io:5349?transport=tcp'],
66+
username: 'turnuser',
67+
credential: TURN_PASSWORD,
68+
},
5269
],
53-
iceCandidatePoolSize: 10,
54-
};
70+
iceCandidatePoolSize: 0,
71+
};
5572

5673
let webSocketMessage = new WebSocketMessage(REGISTER, "", "", "");
5774

@@ -120,7 +137,7 @@ function handleOffer(offer) {
120137
if (rtcPeerConnection) {
121138
rtcPeerConnection.close();
122139
}
123-
rtcPeerConnection = new RTCPeerConnection();
140+
rtcPeerConnection = new RTCPeerConnection(STUN_SERVERS);
124141
rtcPeerConnection.onicecandidate = (e) => {
125142
if (!e.candidate) {
126143
log("all candidates have been generated, now sending answer...");
@@ -132,8 +149,8 @@ function handleOffer(offer) {
132149
dataChannel = e.channel;
133150
setupDataChannelHandlersForSendingFile(dataChannel);
134151
};
135-
rtcPeerConnection.setRemoteDescription(offer.message).then(log("offer set, establishing p2p conn..."));
136-
rtcPeerConnection.createAnswer().then((a) => rtcPeerConnection.setLocalDescription(a).then(log("answer created")));
152+
rtcPeerConnection.setRemoteDescription(offer.message).then(() => log("offer set, establishing p2p conn..."));
153+
rtcPeerConnection.createAnswer().then((a) => rtcPeerConnection.setLocalDescription(a).then(() => log("answer created")));
137154
}
138155

139156
function handleAnswer(answer) {
@@ -166,7 +183,7 @@ function initiateOffer(senderUsername, receiverUsername) {
166183
sendMessage(OFFER, senderUsername, receiverUsername, rtcPeerConnection.localDescription);
167184
}
168185
}
169-
rtcPeerConnection.createOffer().then((o) => rtcPeerConnection.setLocalDescription(o).then(log("offer created, establishing p2p")));
186+
rtcPeerConnection.createOffer().then((o) => rtcPeerConnection.setLocalDescription(o).then(() => log("offer created, establishing p2p")));
170187

171188
}
172189

0 commit comments

Comments
 (0)