example-multipleServers-pretty.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <link rel="shortcut icon" href="favicon.ico">
  5. <meta charset="UTF-8" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, user-scalable=no" />
  7. <title>LibreSpeed Example</title>
  8. <script type="text/javascript" src="speedtest.js"></script>
  9. <script type="text/javascript">
  10. //LIST OF TEST SERVERS. See documentation for details if needed
  11. var SPEEDTEST_SERVERS=[
  12. { //this is my demo server, remove it
  13. name:"Speedtest Demo Server (Helsinki)", //user friendly name for the server
  14. server:"//fi.openspeed.org/", //URL to the server. // at the beginning will be replaced with http:// or https:// automatically
  15. dlURL:"garbage.php", //path to download test on this server (garbage.php or replacement)
  16. ulURL:"empty.php", //path to upload test on this server (empty.php or replacement)
  17. pingURL:"empty.php", //path to ping/jitter test on this server (empty.php or replacement)
  18. getIpURL:"getIP.php" //path to getIP on this server (getIP.php or replacement)
  19. },
  20. { //this is my demo server, remove it
  21. name:"Old Speedtest Demo Server",
  22. server:"//mpotdemo.fdossena.com/",
  23. dlURL:"garbage.php",
  24. ulURL:"empty.php",
  25. pingURL:"empty.php",
  26. getIpURL:"getIP.php"
  27. }
  28. //add other servers here, comma separated
  29. ];
  30. //INITIALIZE SPEEDTEST
  31. var s=new Speedtest(); //create speedtest object
  32. s.onupdate=function(data){ //callback to update data in UI
  33. I("ip").textContent=data.clientIp;
  34. I("dlText").textContent=(data.testState==1&&data.dlStatus==0)?"...":data.dlStatus;
  35. I("ulText").textContent=(data.testState==3&&data.ulStatus==0)?"...":data.ulStatus;
  36. I("pingText").textContent=data.pingStatus;
  37. I("jitText").textContent=data.jitterStatus;
  38. }
  39. s.onend=function(aborted){ //callback for test ended/aborted
  40. I("startStopBtn").className=""; //show start button again
  41. if(aborted){ //if the test was aborted, clear the UI and prepare for new test
  42. initUI();
  43. }
  44. }
  45. function selectServer(){ //called after loading server list
  46. s.selectServer(function(server){ //run server selection. When the server has been selected, display it in the UI
  47. I("startStopBtn").style.display=""; //show start/stop button again
  48. I("serverId").textContent=server.name; //show name of test server
  49. });
  50. }
  51. function loadServers(){ //called when the page is fully loaded
  52. I("startStopBtn").style.display="none"; //hide start/stop button during server selection
  53. if(typeof SPEEDTEST_SERVERS === "string"){
  54. //load servers from url
  55. s.loadServerList(SPEEDTEST_SERVERS,function(servers){
  56. //list loaded
  57. SPEEDTEST_SERVERS=servers;
  58. selectServer();
  59. });
  60. }else{
  61. //hardcoded list of servers, already loaded
  62. s.addTestPoints(SPEEDTEST_SERVERS);
  63. selectServer();
  64. }
  65. }
  66. function startStop(){ //start/stop button pressed
  67. if(s.getState()==3){
  68. //speedtest is running, abort
  69. s.abort();
  70. }else{
  71. //test is not running, begin
  72. s.start();
  73. I("startStopBtn").className="running";
  74. }
  75. }
  76. //function to (re)initialize UI
  77. function initUI(){
  78. I("dlText").textContent="";
  79. I("ulText").textContent="";
  80. I("pingText").textContent="";
  81. I("jitText").textContent="";
  82. I("ip").textContent="";
  83. }
  84. function I(id){return document.getElementById(id);}
  85. </script>
  86. <style type="text/css">
  87. html,body{
  88. border:none; padding:0; margin:0;
  89. background:#FFFFFF;
  90. color:#202020;
  91. }
  92. body{
  93. text-align:center;
  94. font-family:"Roboto",sans-serif;
  95. }
  96. h1{
  97. color:#404040;
  98. }
  99. #startStopBtn{
  100. display:inline-block;
  101. margin:0 auto;
  102. color:#6060AA;
  103. background-color:rgba(0,0,0,0);
  104. border:0.15em solid #6060FF;
  105. border-radius:0.3em;
  106. transition:all 0.3s;
  107. box-sizing:border-box;
  108. width:8em; height:3em;
  109. line-height:2.7em;
  110. cursor:pointer;
  111. box-shadow: 0 0 0 rgba(0,0,0,0.1), inset 0 0 0 rgba(0,0,0,0.1);
  112. }
  113. #startStopBtn:hover{
  114. box-shadow: 0 0 2em rgba(0,0,0,0.1), inset 0 0 1em rgba(0,0,0,0.1);
  115. }
  116. #startStopBtn.running{
  117. background-color:#FF3030;
  118. border-color:#FF6060;
  119. color:#FFFFFF;
  120. }
  121. #startStopBtn:before{
  122. content:"Start";
  123. }
  124. #startStopBtn.running:before{
  125. content:"Abort";
  126. }
  127. #test{
  128. margin-top:2em;
  129. margin-bottom:12em;
  130. }
  131. div.testArea{
  132. display:inline-block;
  133. width:14em;
  134. height:9em;
  135. position:relative;
  136. box-sizing:border-box;
  137. }
  138. div.testName{
  139. position:absolute;
  140. top:0.1em; left:0;
  141. width:100%;
  142. font-size:1.4em;
  143. z-index:9;
  144. }
  145. div.meterText{
  146. position:absolute;
  147. bottom:1.5em; left:0;
  148. width:100%;
  149. font-size:2.5em;
  150. z-index:9;
  151. }
  152. #dlText{
  153. color:#6060AA;
  154. }
  155. #ulText{
  156. color:#309030;
  157. }
  158. #pingText,#jitText{
  159. color:#AA6060;
  160. }
  161. div.meterText:empty:before{
  162. color:#505050 !important;
  163. content:"0.00";
  164. }
  165. div.unit{
  166. position:absolute;
  167. bottom:2em; left:0;
  168. width:100%;
  169. z-index:9;
  170. }
  171. div.testGroup{
  172. display:inline-block;
  173. }
  174. @media all and (max-width:65em){
  175. body{
  176. font-size:1.5vw;
  177. }
  178. }
  179. @media all and (max-width:40em){
  180. body{
  181. font-size:0.8em;
  182. }
  183. div.testGroup{
  184. display:block;
  185. margin: 0 auto;
  186. }
  187. }
  188. </style>
  189. </head>
  190. <body>
  191. <h1>LibreSpeed Example</h1>
  192. <div id="startStopBtn" onclick="startStop()"></div>
  193. <div id="serverId">Selecting server...</div>
  194. <div id="test">
  195. <div class="testGroup">
  196. <div class="testArea">
  197. <div class="testName">Download</div>
  198. <div id="dlText" class="meterText"></div>
  199. <div class="unit">Mbps</div>
  200. </div>
  201. <div class="testArea">
  202. <div class="testName">Upload</div>
  203. <div id="ulText" class="meterText"></div>
  204. <div class="unit">Mbps</div>
  205. </div>
  206. </div>
  207. <div class="testGroup">
  208. <div class="testArea">
  209. <div class="testName">Ping</div>
  210. <div id="pingText" class="meterText"></div>
  211. <div class="unit">ms</div>
  212. </div>
  213. <div class="testArea">
  214. <div class="testName">Jitter</div>
  215. <div id="jitText" class="meterText"></div>
  216. <div class="unit">ms</div>
  217. </div>
  218. </div>
  219. <div id="ipArea">
  220. IP Address: <span id="ip"></span>
  221. </div>
  222. </div>
  223. <a href="https://github.com/librespeed/speedtest">Source code</a>
  224. <script type="text/javascript">
  225. initUI();
  226. loadServers();
  227. </script>
  228. </body>
  229. </html>