server_selector.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. if(server.server.indexOf(location.protocol)==-1) done(); else{
  43. var nextPing=function(){
  44. if(i++==PINGS){done(); return;}
  45. ping(server.server+server.pingURL,function(t){
  46. if(t>=0){
  47. if(t<server.pingT||server.pingT==-1) server.pingT=t;
  48. if(t<SLOW_THRESHOLD) nextPing(); else done();
  49. }else done();
  50. }.bind(this));
  51. }.bind(this);
  52. nextPing();
  53. }
  54. }
  55. /*this function goes through a list of servers, each with this format:
  56. {
  57. name: "User friendly name",
  58. server:"http://yourBackend.com/", <---- make sure there's a / at the end!
  59. dlURL:"garbage.php" <----- path to garbage.php or its replacement on the server
  60. ulURL:"empty.php" <----- path to empty.php or its replacement on the server
  61. pingURL:"empty.php" <----- path to empty.php or its replacement on the server. This is used to ping the server by this selector
  62. getIpURL:"getIP.php" <----- path to getIP.php or its replacement on the server
  63. }
  64. 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.
  65. */
  66. function selectServer(serverList,result){
  67. var i=0;
  68. var done=function(){
  69. var bestServer=null;
  70. for(var i=0;i<serverList.length;i++){
  71. if(serverList[i].pingT!=-1&&(bestServer==null||serverList[i].pingT<bestServer.pingT)) bestServer=serverList[i];
  72. }
  73. result(bestServer);
  74. }.bind(this);
  75. var nextServer=function(){
  76. if(i==serverList.length){done(); return;}
  77. checkServer(serverList[i++],nextServer);
  78. }.bind(this);
  79. nextServer();
  80. }
  81. /*
  82. this function is a faster version of selectServer that tests multiple servers concurrently. Useful for large server lists
  83. */
  84. var CONCURRENCY=4; //4 seems to be the safest value
  85. function fastSelectServer(serverList,result){
  86. var serverLists=[];
  87. for(var i=0;i<CONCURRENCY;i++){
  88. serverLists[i]=[];
  89. }
  90. for(var i=0;i<serverList.length;i++){
  91. serverLists[i%CONCURRENCY].push(serverList[i]);
  92. }
  93. var completed=0;
  94. var bestServer=null;
  95. for(var i=0;i<CONCURRENCY;i++){
  96. selectServer(serverLists[i],function(server){
  97. if(server!=null){
  98. if(bestServer==null||server.pingT<bestServer.pingT) bestServer=server;
  99. }
  100. completed++;
  101. if(completed==CONCURRENCY) result(bestServer);
  102. }.bind(this));
  103. }
  104. }