Server list can now be loaded from a JSON file instead of hardcoding it
This commit is contained in:
parent
4a1ea40b32
commit
bfe80d9fb9
|
@ -32,10 +32,13 @@ var SPEEDTEST_SERVERS=[
|
||||||
//INITIALIZE SPEEDTEST
|
//INITIALIZE SPEEDTEST
|
||||||
var s=new Speedtest(); //create speedtest object
|
var s=new Speedtest(); //create speedtest object
|
||||||
s.setParameter("telemetry_level","basic"); //enable telemetry
|
s.setParameter("telemetry_level","basic"); //enable telemetry
|
||||||
s.addTestPoints(SPEEDTEST_SERVERS); //add list of servers
|
|
||||||
|
|
||||||
//SERVER AUTO SELECTION
|
//SERVER AUTO SELECTION
|
||||||
function initServers(){
|
function initServers(){
|
||||||
|
var noServersAvailable=function(){
|
||||||
|
I("message").innerHTML="No servers available";
|
||||||
|
}
|
||||||
|
var runServerSelect=function(){
|
||||||
s.selectServer(function(server){
|
s.selectServer(function(server){
|
||||||
if(server!=null){ //at least 1 server is available
|
if(server!=null){ //at least 1 server is available
|
||||||
I("loading").className="hidden"; //hide loading message
|
I("loading").className="hidden"; //hide loading message
|
||||||
|
@ -52,10 +55,26 @@ function initServers(){
|
||||||
I("testWrapper").className="visible";
|
I("testWrapper").className="visible";
|
||||||
initUI();
|
initUI();
|
||||||
}else{ //no servers are available, the test cannot proceed
|
}else{ //no servers are available, the test cannot proceed
|
||||||
I("message").innerHTML="No servers available";
|
noServersAvailable();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if(typeof SPEEDTEST_SERVERS === "string"){
|
||||||
|
//need to fetch list of servers from specified URL
|
||||||
|
s.loadServerList(SPEEDTEST_SERVERS,function(servers){
|
||||||
|
if(servers==null){ //failed to load server list
|
||||||
|
noServersAvailable();
|
||||||
|
}else{ //server list loaded
|
||||||
|
SPEEDTEST_SERVERS=servers;
|
||||||
|
runServerSelect();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}else{
|
||||||
|
//hardcoded server list
|
||||||
|
s.addTestPoints(SPEEDTEST_SERVERS);
|
||||||
|
runServerSelect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var meterBk=/Trident.*rv:(\d+\.\d+)/i.test(navigator.userAgent)?"#EAEAEA":"#80808040";
|
var meterBk=/Trident.*rv:(\d+\.\d+)/i.test(navigator.userAgent)?"#EAEAEA":"#80808040";
|
||||||
var dlColor="#6060AA",
|
var dlColor="#6060AA",
|
||||||
|
|
|
@ -32,7 +32,7 @@ var SPEEDTEST_SERVERS=[
|
||||||
|
|
||||||
//INITIALIZE SPEEDTEST
|
//INITIALIZE SPEEDTEST
|
||||||
var s=new Speedtest(); //create speedtest object
|
var s=new Speedtest(); //create speedtest object
|
||||||
s.addTestPoints(SPEEDTEST_SERVERS); //add list of servers
|
|
||||||
s.onupdate=function(data){ //callback to update data in UI
|
s.onupdate=function(data){ //callback to update data in UI
|
||||||
I("ip").textContent=data.clientIp;
|
I("ip").textContent=data.clientIp;
|
||||||
I("dlText").textContent=(data.testState==1&&data.dlStatus==0)?"...":data.dlStatus;
|
I("dlText").textContent=(data.testState==1&&data.dlStatus==0)?"...":data.dlStatus;
|
||||||
|
@ -46,13 +46,29 @@ s.onend=function(aborted){ //callback for test ended/aborted
|
||||||
initUI();
|
initUI();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function selectServer(){ //called when the page is fully loaded
|
function selectServer(){ //called after loading server list
|
||||||
I("startStopBtn").style.display="none"; //hide start/stop button during server selection
|
|
||||||
s.selectServer(function(server){ //run server selection. When the server has been selected, display it in the UI
|
s.selectServer(function(server){ //run server selection. When the server has been selected, display it in the UI
|
||||||
I("startStopBtn").style.display=""; //show start/stop button again
|
I("startStopBtn").style.display=""; //show start/stop button again
|
||||||
I("serverId").textContent=server.name; //show name of test server
|
I("serverId").textContent=server.name; //show name of test server
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
function loadServers(){ //called when the page is fully loaded
|
||||||
|
I("startStopBtn").style.display="none"; //hide start/stop button during server selection
|
||||||
|
if(typeof SPEEDTEST_SERVERS === "string"){
|
||||||
|
//load servers from url
|
||||||
|
s.loadServerList(SPEEDTEST_SERVERS,function(servers){
|
||||||
|
//list loaded
|
||||||
|
SPEEDTEST_SERVERS=servers;
|
||||||
|
selectServer();
|
||||||
|
});
|
||||||
|
}else{
|
||||||
|
//hardcoded list of servers, already loaded
|
||||||
|
s.addTestPoints(SPEEDTEST_SERVERS);
|
||||||
|
selectServer();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function startStop(){ //start/stop button pressed
|
function startStop(){ //start/stop button pressed
|
||||||
|
@ -219,7 +235,7 @@ function I(id){return document.getElementById(id);}
|
||||||
<a href="https://github.com/librespeed/speedtest">Source code</a>
|
<a href="https://github.com/librespeed/speedtest">Source code</a>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
initUI();
|
initUI();
|
||||||
selectServer();
|
loadServers();
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
36
speedtest.js
36
speedtest.js
|
@ -49,7 +49,7 @@ function Speedtest() {
|
||||||
this._settings = {}; //settings for the speedtest worker
|
this._settings = {}; //settings for the speedtest worker
|
||||||
this._state = 0; //0=adding settings, 1=adding servers, 2=server selection done, 3=test running, 4=done
|
this._state = 0; //0=adding settings, 1=adding servers, 2=server selection done, 3=test running, 4=done
|
||||||
console.log(
|
console.log(
|
||||||
"LibreSpeed by Federico Dossena v5.1 - https://github.com/librespeed/speedtest"
|
"LibreSpeed by Federico Dossena v5.2 - https://github.com/librespeed/speedtest"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,6 +127,40 @@ Speedtest.prototype = {
|
||||||
addTestPoints: function(list) {
|
addTestPoints: function(list) {
|
||||||
for (var i = 0; i < list.length; i++) this.addTestPoint(list[i]);
|
for (var i = 0; i < list.length; i++) this.addTestPoint(list[i]);
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* Load a JSON server list from URL (multiple points of test)
|
||||||
|
* url: the url where the server list can be fetched. Must be an array with objects containing the following elements:
|
||||||
|
* {
|
||||||
|
* "name": "User friendly name",
|
||||||
|
* "server":"http://yourBackend.com/", URL to your server. You can specify http:// or https://. If your server supports both, just write // without the protocol
|
||||||
|
* "dlURL":"garbage.php" path to garbage.php or its replacement on the server
|
||||||
|
* "ulURL":"empty.php" path to empty.php or its replacement on the server
|
||||||
|
* "pingURL":"empty.php" path to empty.php or its replacement on the server. This is used to ping the server by this selector
|
||||||
|
* "getIpURL":"getIP.php" path to getIP.php or its replacement on the server
|
||||||
|
* }
|
||||||
|
* result: callback to be called when the list is loaded correctly. An array with the loaded servers will be passed to this function, or null if it failed
|
||||||
|
*/
|
||||||
|
loadServerList: function(url,result) {
|
||||||
|
if (this._state == 0) this._state = 1;
|
||||||
|
if (this._state != 1) throw "You can't add a server after server selection";
|
||||||
|
this._settings.mpot = true;
|
||||||
|
var xhr = new XMLHttpRequest();
|
||||||
|
xhr.onload = function(){
|
||||||
|
try{
|
||||||
|
var servers=JSON.parse(xhr.responseText);
|
||||||
|
for(var i=0;i<servers.length;i++){
|
||||||
|
this._checkServerDefinition(servers[i]);
|
||||||
|
}
|
||||||
|
this.addTestPoints(servers);
|
||||||
|
result(servers);
|
||||||
|
}catch(e){
|
||||||
|
result(null);
|
||||||
|
}
|
||||||
|
}.bind(this);
|
||||||
|
xhr.onerror = function(){result(null);}
|
||||||
|
xhr.open("GET",url);
|
||||||
|
xhr.send();
|
||||||
|
},
|
||||||
/**
|
/**
|
||||||
* Returns the selected server (multiple points of test)
|
* Returns the selected server (multiple points of test)
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue