Improved JS formatting
This commit is contained in:
parent
58970adcb8
commit
697b4c85a6
|
@ -8,14 +8,16 @@
|
||||||
//pings the specified URL, then calls the function result. Result will receive a parameter which is either the time it took to ping the URL, or -1 if something went wrong.
|
//pings the specified URL, then calls the function result. Result will receive a parameter which is either the time it took to ping the URL, or -1 if something went wrong.
|
||||||
var PING_TIMEOUT = 1000;
|
var PING_TIMEOUT = 1000;
|
||||||
var USE_PING_TIMEOUT = true; //will be disabled on unsupported browsers
|
var USE_PING_TIMEOUT = true; //will be disabled on unsupported browsers
|
||||||
if(/MSIE.(\d+\.\d+)/i.test(navigator.userAgent)){ //IE11 doesn't support XHR timeout
|
if (/MSIE.(\d+\.\d+)/i.test(navigator.userAgent)) {
|
||||||
|
//IE11 doesn't support XHR timeout
|
||||||
USE_PING_TIMEOUT = false;
|
USE_PING_TIMEOUT = false;
|
||||||
}
|
}
|
||||||
function ping(url, result) {
|
function ping(url, result) {
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
var t = new Date().getTime();
|
var t = new Date().getTime();
|
||||||
xhr.onload = function() {
|
xhr.onload = function() {
|
||||||
if(xhr.responseText.length==0){ //we expect an empty response
|
if (xhr.responseText.length == 0) {
|
||||||
|
//we expect an empty response
|
||||||
var instspd = new Date().getTime() - t; //rough timing estimate
|
var instspd = new Date().getTime() - t; //rough timing estimate
|
||||||
try {
|
try {
|
||||||
//try to get more accurate timing using performance API
|
//try to get more accurate timing using performance API
|
||||||
|
@ -24,8 +26,7 @@ function ping(url,result){
|
||||||
var d = p.responseStart - p.requestStart;
|
var d = p.responseStart - p.requestStart;
|
||||||
if (d <= 0) d = p.duration;
|
if (d <= 0) d = p.duration;
|
||||||
if (d > 0 && d < instspd) instspd = d;
|
if (d > 0 && d < instspd) instspd = d;
|
||||||
}catch(e){
|
} catch (e) {}
|
||||||
}
|
|
||||||
result(instspd);
|
result(instspd);
|
||||||
} else result(-1);
|
} else result(-1);
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
|
@ -48,15 +49,23 @@ var PINGS=3, //up to 3 pings are performed, unless the server is down...
|
||||||
function checkServer(server, done) {
|
function checkServer(server, done) {
|
||||||
var i = 0;
|
var i = 0;
|
||||||
server.pingT = -1;
|
server.pingT = -1;
|
||||||
if(server.server.indexOf(location.protocol)==-1) done(); else{
|
if (server.server.indexOf(location.protocol) == -1) done();
|
||||||
|
else {
|
||||||
var nextPing = function() {
|
var nextPing = function() {
|
||||||
if(i++==PINGS){done(); return;}
|
if (i++ == PINGS) {
|
||||||
ping(server.server+server.pingURL,function(t){
|
done();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ping(
|
||||||
|
server.server + server.pingURL,
|
||||||
|
function(t) {
|
||||||
if (t >= 0) {
|
if (t >= 0) {
|
||||||
if (t < server.pingT || server.pingT == -1) server.pingT = t;
|
if (t < server.pingT || server.pingT == -1) server.pingT = t;
|
||||||
if(t<SLOW_THRESHOLD) nextPing(); else done();
|
if (t < SLOW_THRESHOLD) nextPing();
|
||||||
|
else done();
|
||||||
} else done();
|
} else done();
|
||||||
}.bind(this));
|
}.bind(this)
|
||||||
|
);
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
nextPing();
|
nextPing();
|
||||||
}
|
}
|
||||||
|
@ -83,7 +92,10 @@ function selectServer(serverList,result){
|
||||||
result(bestServer);
|
result(bestServer);
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
var nextServer = function() {
|
var nextServer = function() {
|
||||||
if(i==serverList.length){done(); return;}
|
if (i == serverList.length) {
|
||||||
|
done();
|
||||||
|
return;
|
||||||
|
}
|
||||||
checkServer(serverList[i++], nextServer);
|
checkServer(serverList[i++], nextServer);
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
nextServer();
|
nextServer();
|
||||||
|
@ -104,12 +116,15 @@ function fastSelectServer(serverList,result){
|
||||||
var completed = 0;
|
var completed = 0;
|
||||||
var bestServer = null;
|
var bestServer = null;
|
||||||
for (var i = 0; i < CONCURRENCY; i++) {
|
for (var i = 0; i < CONCURRENCY; i++) {
|
||||||
selectServer(serverLists[i],function(server){
|
selectServer(
|
||||||
|
serverLists[i],
|
||||||
|
function(server) {
|
||||||
if (server != null) {
|
if (server != null) {
|
||||||
if (bestServer == null || server.pingT < bestServer.pingT) bestServer = server;
|
if (bestServer == null || server.pingT < bestServer.pingT) bestServer = server;
|
||||||
}
|
}
|
||||||
completed++;
|
completed++;
|
||||||
if (completed == CONCURRENCY) result(bestServer);
|
if (completed == CONCURRENCY) result(bestServer);
|
||||||
}.bind(this));
|
}.bind(this)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue