|
1 | 1 | "use strict";
|
2 | 2 |
|
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; |
11 | 8 | resolve(false);
|
12 | 9 | promiseResolved = true;
|
13 | 10 | }, timeout || 5000);
|
14 | 11 |
|
15 |
| - let promiseResolved = false, |
| 12 | + var promiseResolved = false, |
16 | 13 | myPeerConnection =
|
17 | 14 | window.RTCPeerConnection ||
|
18 | 15 | window.mozRTCPeerConnection ||
|
19 | 16 | window.webkitRTCPeerConnection, //compatibility for firefox and chrome
|
20 | 17 | pc = new myPeerConnection({ iceServers: [turnConfig] }),
|
21 |
| - noop = function() {}; |
| 18 | + noop = () => {}; |
22 | 19 | pc.createDataChannel(""); //create a bogus data channel
|
23 |
| - pc.createOffer(function(sdp) { |
| 20 | + pc.createOffer(sdp => { |
24 | 21 | if (sdp.sdp.indexOf("typ relay") > -1) {
|
25 | 22 | // sometimes sdp contains the ice candidates...
|
26 |
| - promiseResolved = "TURN"; |
| 23 | + promiseResolved = true; |
27 | 24 | resolve(true);
|
28 | 25 | }
|
29 | 26 | pc.setLocalDescription(sdp, noop, noop);
|
30 | 27 | }, noop); // create offer and set local description
|
31 |
| - pc.onicecandidate = function(ice) { |
| 28 | + pc.onicecandidate = ice => { |
32 | 29 | //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); |
45 | 40 | };
|
46 | 41 | });
|
47 |
| -} |
| 42 | +}; |
48 | 43 |
|
49 |
| -$(document).ready(function() { |
50 |
| - $("#form").submit(function(e) { |
| 44 | +document.addEventListener("DOMContentLoaded", () => { |
| 45 | + const form = document.getElementById("form"); |
| 46 | + form.addEventListener("submit", e => { |
51 | 47 | e.preventDefault();
|
52 | 48 | e.stopPropagation();
|
53 | 49 | });
|
54 | 50 |
|
55 | 51 | $("#form")
|
56 | 52 | .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 = {}; |
60 | 56 | for (let i = 0; i < e.fields.length; i++) {
|
61 | 57 | info[e.fields[i].element.name] =
|
62 | 58 | e.fields[i].element.name == "timeout"
|
63 | 59 | ? Number(e.fields[i].value)
|
64 | 60 | : e.fields[i].value;
|
65 | 61 | }
|
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'>×" |
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'>×" |
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'>× </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'>×</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 | + } |
114 | 98 | });
|
115 | 99 | });
|
0 commit comments