Skip to content

Commit 8e166df

Browse files
Corregido comprobador STUN/TURN server.
1 parent 154c16f commit 8e166df

File tree

1 file changed

+63
-79
lines changed

1 file changed

+63
-79
lines changed

js/script.js

Lines changed: 63 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,99 @@
11
"use strict";
22

3-
//Autor's URL of JS function: https://stackoverflow.com/a/57003981
4-
function checkTurnOrStun(turnConfig, timeout) {
5-
return new Promise(function(resolve, reject) {
6-
setTimeout(function() {
7-
if (promiseResolved) {
8-
if (promiseResolved == "STUN") resolve("STUN");
9-
return;
10-
}
3+
//Autor's URL of checkTurnOrStun function: https://stackoverflow.com/a/34033938
4+
const checkTurnOrStun = (turnConfig, timeout) => {
5+
return new Promise((resolve, reject) => {
6+
setTimeout(() => {
7+
if (promiseResolved) return;
118
resolve(false);
129
promiseResolved = true;
1310
}, timeout || 5000);
1411

15-
let promiseResolved = false,
12+
var promiseResolved = false,
1613
myPeerConnection =
1714
window.RTCPeerConnection ||
1815
window.mozRTCPeerConnection ||
1916
window.webkitRTCPeerConnection, //compatibility for firefox and chrome
2017
pc = new myPeerConnection({ iceServers: [turnConfig] }),
21-
noop = function() {};
18+
noop = () => {};
2219
pc.createDataChannel(""); //create a bogus data channel
23-
pc.createOffer(function(sdp) {
20+
pc.createOffer(sdp => {
2421
if (sdp.sdp.indexOf("typ relay") > -1) {
2522
// sometimes sdp contains the ice candidates...
26-
promiseResolved = "TURN";
23+
promiseResolved = true;
2724
resolve(true);
2825
}
2926
pc.setLocalDescription(sdp, noop, noop);
3027
}, noop); // create offer and set local description
31-
pc.onicecandidate = function(ice) {
28+
pc.onicecandidate = ice => {
3229
//listen for candidate events
33-
if (!ice || !ice.candidate || !ice.candidate.candidate) return;
34-
if (ice.candidate.candidate.indexOf("typ relay") != -1) {
35-
promiseResolved = "TURN";
36-
resolve("TURN");
37-
} else if (
38-
!promiseResolved &&
39-
(ice.candidate.candidate.indexOf("typ prflx") != -1 ||
40-
ice.candidate.candidate.indexOf("typ srflx") != -1)
41-
) {
42-
promiseResolved = "STUN";
43-
if (turnConfig.url.indexOf("turn:") !== 0) resolve("STUN");
44-
} else return;
30+
if (
31+
promiseResolved ||
32+
!ice ||
33+
!ice.candidate ||
34+
!ice.candidate.candidate ||
35+
!(ice.candidate.candidate.indexOf("typ relay") > -1)
36+
)
37+
return;
38+
promiseResolved = true;
39+
resolve(true);
4540
};
4641
});
47-
}
42+
};
4843

49-
$(document).ready(function() {
50-
$("#form").submit(function(e) {
44+
document.addEventListener("DOMContentLoaded", () => {
45+
const form = document.getElementById("form");
46+
form.addEventListener("submit", e => {
5147
e.preventDefault();
5248
e.stopPropagation();
5349
});
5450

5551
$("#form")
5652
.parsley()
57-
.on("form:success", function(e) {
58-
$("#form #btnSubmit").prop("disabled", true);
59-
let info = {};
53+
.on("form:success", async e => {
54+
form.querySelector("#btnSubmit").disabled = true;
55+
const info = {};
6056
for (let i = 0; i < e.fields.length; i++) {
6157
info[e.fields[i].element.name] =
6258
e.fields[i].element.name == "timeout"
6359
? Number(e.fields[i].value)
6460
: e.fields[i].value;
6561
}
66-
67-
checkTurnOrStun(
68-
info.usuario != "" && info.password != ""
69-
? {
70-
url: `${info.tipo_servidor}:${info.host}:${info.puerto}?transport=${info.protocolo}`,
71-
credential: info.password,
72-
username: info.usuario
73-
}
74-
: {
75-
url: `${info.tipo_servidor}:${info.host}:${info.puerto}?transport=${info.protocolo}`
76-
},
77-
info.timeout
78-
)
79-
.then(function(resultado) {
80-
if (resultado) {
81-
$("#success").html("<div class='alert alert-success'>");
82-
$("#success > .alert-success")
83-
.html(
84-
"<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;"
85-
)
86-
.append("</button>");
87-
$("#success > .alert-success").append(
88-
`<strong>El servidor ${resultado}, se encuentra activo!</strong>`
89-
);
90-
$("#success > .alert-success").append("</div>");
91-
} else {
92-
$("#success").html("<div class='alert alert-danger'>");
93-
$("#success > .alert-danger")
94-
.html(
95-
"<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;"
96-
)
97-
.append("</button>");
98-
$("#success > .alert-danger").append(
99-
$("<strong>").text(`El servidor, NO se encuentra activo.`)
100-
);
101-
$("#success > .alert-danger").append("</div>");
102-
}
103-
$("#form").trigger("reset");
104-
$("#form")
105-
.parsley()
106-
.reset();
107-
setTimeout(function() {
108-
$("#form #btnSubmit").prop("disabled", false);
109-
}, 1000);
110-
})
111-
.catch(function(error) {
112-
console.error(error);
113-
});
62+
try {
63+
let success = await checkTurnOrStun(
64+
String(info.usuario).trim().length > 0 &&
65+
String(info.password).trim().length > 0
66+
? {
67+
urls: `${info.tipo_servidor}:${info.host}:${info.puerto}?transport=${info.protocolo}`,
68+
username: info.usuario,
69+
credential: info.password
70+
}
71+
: {
72+
url: `${info.tipo_servidor}:${info.host}:${info.puerto}?transport=${info.protocolo}`
73+
},
74+
info.timeout
75+
);
76+
if (success) {
77+
const divSuccess = document.createElement("div");
78+
divSuccess.className = "alert alert-success";
79+
divSuccess.innerHTML = `<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times; </button><strong>El servidor ${success}, se encuentra activo!</strong>`;
80+
form.querySelector("#success").appendChild(divSuccess);
81+
} else {
82+
const divError = document.createElement("div");
83+
divError.className = "alert alert-danger";
84+
divError.innerHTML = `<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button> <strong>El servidor, NO se encuentra activo.</strong>`;
85+
form.querySelector("#success").appendChild(divError);
86+
}
87+
} catch (error) {
88+
console.error();
89+
} finally {
90+
form.reset();
91+
$("#form")
92+
.parsley()
93+
.reset();
94+
setTimeout(() => {
95+
form.querySelector("#btnSubmit").disabled = false;
96+
}, 1000);
97+
}
11498
});
11599
});

0 commit comments

Comments
 (0)