server_selector.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. HTML5 Speedtest v4.6.1 MPOT - Server selector
  3. by Federico Dossena
  4. https://github.com/adolfintel/speedtest/
  5. GNU LGPLv3 License
  6. */
  7. //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.
  8. //var PING_TIMEOUT=500; //disabled because it breaks IE11
  9. function ping(url,result){
  10. var xhr=new XMLHttpRequest();
  11. var t=new Date().getTime();
  12. xhr.onload=function(){
  13. if(xhr.responseText.length==0){ //we expect an empty response
  14. var instspd=new Date().getTime()-t; //rough timing estimate
  15. try{
  16. //try to get more accurate timing using performance API
  17. var p=performance.getEntries();
  18. p=p[p.length-1];
  19. var d=p.responseStart-p.requestStart;
  20. if(d<=0) d=p.duration;
  21. if(d>0&&d<instspd) instspd=d;
  22. }catch(e){
  23. }
  24. result(instspd);
  25. }else result(-1);
  26. }.bind(this);
  27. xhr.onerror=function(){
  28. result(-1);
  29. }.bind(this);
  30. xhr.open("GET",url);
  31. /*try{ //disabled because it breaks IE11
  32. xhr.timeout=PING_TIMEOUT;
  33. }catch(e){}*/
  34. xhr.send();
  35. }
  36. //this function repeatedly pings a server to get a good estimate of the ping. When it's done, it calls the done function without parameters. At the end of the execution, the server will have a new parameter called pingT, which is either the best ping we got from the server or -1 if something went wrong.
  37. var PINGS=3, //up to 3 pings are performed, unless the server is down...
  38. SLOW_THRESHOLD=500; //...or one of the pings is above this threshold
  39. function checkServer(server,done){
  40. var i=0;
  41. server.pingT=-1;
  42. var nextPing=function(){
  43. if(i++==PINGS){done(); return;}
  44. ping(server.server+server.pingURL,function(t){
  45. if(t>=0){
  46. if(t<server.pingT||server.pingT==-1) server.pingT=t;
  47. if(t<SLOW_THRESHOLD) nextPing(); else done();
  48. }else done();
  49. }.bind(this));
  50. }.bind(this);
  51. nextPing();
  52. }
  53. /*this function goes through a list of servers, each with this format:
  54. {
  55. name: "User friendly name",
  56. server:"http://yourBackend.com/", <---- make sure there's a / at the end!
  57. dlURL:"garbage.php" <----- path to garbage.php or its replacement on the server
  58. ulURL:"empty.php" <----- path to empty.php or its replacement on the server
  59. pingURL:"empty.php" <----- path to empty.php or its replacement on the server. This is used to ping the server by this selector
  60. getIpURL:"getIP.php" <----- path to getIP.php or its replacement on the server
  61. }
  62. For each server, the ping is measured, then the server with the function result is called with the best server, or null if all the servers were down.
  63. */
  64. function selectServer(serverList,result){
  65. var i=0;
  66. var done=function(){
  67. var bestServer=null;
  68. for(var i=0;i<serverList.length;i++){
  69. if(serverList[i].pingT!=-1&&(bestServer==null||serverList[i].pingT<bestServer.pingT)) bestServer=serverList[i];
  70. }
  71. result(bestServer);
  72. }.bind(this);
  73. var nextServer=function(){
  74. if(i==serverList.length){done(); return;}
  75. checkServer(serverList[i++],nextServer);
  76. }.bind(this);
  77. nextServer();
  78. }
  79. /*
  80. this function is a faster version of selectServer that tests multiple servers concurrently. Useful for large server lists
  81. */
  82. var CONCURRENCY=4; //4 seems to be the safest value
  83. function fastSelectServer(serverList,result){
  84. var serverLists=[];
  85. for(var i=0;i<CONCURRENCY;i++){
  86. serverLists[i]=[];
  87. }
  88. for(var i=0;i<serverList.length;i++){
  89. serverLists[i%CONCURRENCY].push(serverList[i]);
  90. }
  91. var completed=0;
  92. var bestServer=null;
  93. for(var i=0;i<CONCURRENCY;i++){
  94. selectServer(serverLists[i],function(server){
  95. if(server!=null){
  96. if(bestServer==null||server.pingT<bestServer.pingT) bestServer=server;
  97. }
  98. completed++;
  99. if(completed==CONCURRENCY) result(bestServer);
  100. }.bind(this));
  101. }
  102. }