diff --git a/example-multipleServers-full.html b/example-multipleServers-full.html index 2e53a4f..c1a6cb4 100644 --- a/example-multipleServers-full.html +++ b/example-multipleServers-full.html @@ -32,29 +32,48 @@ var SPEEDTEST_SERVERS=[ //INITIALIZE SPEEDTEST var s=new Speedtest(); //create speedtest object s.setParameter("telemetry_level","basic"); //enable telemetry -s.addTestPoints(SPEEDTEST_SERVERS); //add list of servers //SERVER AUTO SELECTION function initServers(){ - s.selectServer(function(server){ - if(server!=null){ //at least 1 server is available - I("loading").className="hidden"; //hide loading message - //populate server list for manual selection - for(var i=0;iSource code diff --git a/speedtest.js b/speedtest.js index 9df90fd..c2f1b2f 100644 --- a/speedtest.js +++ b/speedtest.js @@ -49,7 +49,7 @@ function Speedtest() { 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 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) { 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