example-multipleServers-pretty.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 server doesn't actually exist, remove it
  13. name:"Example Server 1", //user friendly name for the server
  14. server:"//test1.mydomain.com/", //URL to the server. // at the beginning will be replaced with http:// or https:// automatically
  15. dlURL:"backend/garbage.php", //path to download test on this server (garbage.php or replacement)
  16. ulURL:"backend/empty.php", //path to upload test on this server (empty.php or replacement)
  17. pingURL:"backend/empty.php", //path to ping/jitter test on this server (empty.php or replacement)
  18. getIpURL:"backend/getIP.php" //path to getIP on this server (getIP.php or replacement)
  19. },
  20. { //this server doesn't actually exist, remove it
  21. name:"Example Server 2", //user friendly name for the server
  22. server:"//test2.example.com/", //URL to the server. // at the beginning will be replaced with http:// or https:// automatically
  23. dlURL:"garbage.php", //path to download test on this server (garbage.php or replacement)
  24. ulURL:"empty.php", //path to upload test on this server (empty.php or replacement)
  25. pingURL:"empty.php", //path to ping/jitter test on this server (empty.php or replacement)
  26. getIpURL:"getIP.php" //path to getIP on this server (getIP.php or replacement)
  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. I("startStopBtn").setAttribute("aria-label","Start");
  42. if(aborted){ //if the test was aborted, clear the UI and prepare for new test
  43. initUI();
  44. }
  45. }
  46. function selectServer(){ //called after loading server list
  47. s.selectServer(function(server){ //run server selection. When the server has been selected, display it in the UI
  48. if(server==null){
  49. I("serverId").textContent="No servers available";
  50. }else{
  51. I("startStopBtn").style.display=""; //show start/stop button again
  52. I("serverId").textContent=server.name; //show name of test server
  53. }
  54. });
  55. }
  56. function loadServers(){ //called when the page is fully loaded
  57. I("startStopBtn").style.display="none"; //hide start/stop button during server selection
  58. if(typeof SPEEDTEST_SERVERS === "string"){
  59. //load servers from url
  60. s.loadServerList(SPEEDTEST_SERVERS,function(servers){
  61. //list loaded
  62. SPEEDTEST_SERVERS=servers;
  63. selectServer();
  64. });
  65. }else{
  66. //hardcoded list of servers, already loaded
  67. s.addTestPoints(SPEEDTEST_SERVERS);
  68. selectServer();
  69. }
  70. }
  71. function startStop(){ //start/stop button pressed
  72. if(s.getState()==3){
  73. //speedtest is running, abort
  74. s.abort();
  75. }else{
  76. //test is not running, begin
  77. s.start();
  78. I("startStopBtn").className="running";
  79. I("startStopBtn").setAttribute("aria-label","Abort");
  80. }
  81. }
  82. //function to (re)initialize UI
  83. function initUI(){
  84. I("dlText").textContent="";
  85. I("ulText").textContent="";
  86. I("pingText").textContent="";
  87. I("jitText").textContent="";
  88. I("ip").textContent="";
  89. }
  90. function I(id){return document.getElementById(id);}
  91. </script>
  92. <style type="text/css">
  93. html,body{
  94. border:none; padding:0; margin:0;
  95. background:#FFFFFF;
  96. color:#202020;
  97. }
  98. body{
  99. text-align:center;
  100. font-family:"Roboto",sans-serif;
  101. }
  102. h1{
  103. color:#404040;
  104. }
  105. #startStopBtn{
  106. display:inline-block;
  107. margin:0 auto;
  108. color:#6060AA;
  109. background-color:rgba(0,0,0,0);
  110. border:0.15em solid #6060FF;
  111. padding:0;
  112. font:inherit;
  113. border-radius:0.3em;
  114. transition:all 0.3s;
  115. box-sizing:border-box;
  116. width:8em; height:3em;
  117. line-height:2.7em;
  118. cursor:pointer;
  119. box-shadow: 0 0 0 rgba(0,0,0,0.1), inset 0 0 0 rgba(0,0,0,0.1);
  120. }
  121. #startStopBtn:hover{
  122. box-shadow: 0 0 2em rgba(0,0,0,0.1), inset 0 0 1em rgba(0,0,0,0.1);
  123. }
  124. #startStopBtn.running{
  125. background-color:#FF3030;
  126. border-color:#FF6060;
  127. color:#FFFFFF;
  128. }
  129. #startStopBtn:before{
  130. content:"Start";
  131. }
  132. #startStopBtn.running:before{
  133. content:"Abort";
  134. }
  135. #test{
  136. margin-top:2em;
  137. margin-bottom:12em;
  138. }
  139. div.testArea{
  140. display:inline-block;
  141. width:14em;
  142. height:9em;
  143. position:relative;
  144. box-sizing:border-box;
  145. }
  146. div.testName{
  147. position:absolute;
  148. top:0.1em; left:0;
  149. width:100%;
  150. font-size:1.4em;
  151. z-index:9;
  152. }
  153. div.meterText{
  154. position:absolute;
  155. bottom:1.5em; left:0;
  156. width:100%;
  157. font-size:2.5em;
  158. z-index:9;
  159. }
  160. #dlText{
  161. color:#6060AA;
  162. }
  163. #ulText{
  164. color:#309030;
  165. }
  166. #pingText,#jitText{
  167. color:#AA6060;
  168. }
  169. div.meterText:empty:before{
  170. color:#505050 !important;
  171. content:"0.00";
  172. }
  173. div.unit{
  174. position:absolute;
  175. bottom:2em; left:0;
  176. width:100%;
  177. z-index:9;
  178. }
  179. div.testGroup{
  180. display:inline-block;
  181. }
  182. @media all and (max-width:65em){
  183. body{
  184. font-size:1.5vw;
  185. }
  186. }
  187. @media all and (max-width:40em){
  188. body{
  189. font-size:0.8em;
  190. }
  191. div.testGroup{
  192. display:block;
  193. margin: 0 auto;
  194. }
  195. }
  196. </style>
  197. </head>
  198. <body>
  199. <h1>LibreSpeed Example</h1>
  200. <button id="startStopBtn" onclick="startStop()" aria-label="Start"></button>
  201. <div id="serverId">Selecting server...</div>
  202. <div id="test">
  203. <div class="testGroup">
  204. <div class="testArea">
  205. <div class="testName">Download</div>
  206. <div id="dlText" class="meterText"></div>
  207. <div class="unit">Mbit/s</div>
  208. </div>
  209. <div class="testArea">
  210. <div class="testName">Upload</div>
  211. <div id="ulText" class="meterText"></div>
  212. <div class="unit">Mbit/s</div>
  213. </div>
  214. </div>
  215. <div class="testGroup">
  216. <div class="testArea">
  217. <div class="testName">Ping</div>
  218. <div id="pingText" class="meterText"></div>
  219. <div class="unit">ms</div>
  220. </div>
  221. <div class="testArea">
  222. <div class="testName">Jitter</div>
  223. <div id="jitText" class="meterText"></div>
  224. <div class="unit">ms</div>
  225. </div>
  226. </div>
  227. <div id="ipArea">
  228. IP Address: <span id="ip"></span>
  229. </div>
  230. </div>
  231. <a href="https://github.com/librespeed/speedtest">Source code</a>
  232. <script type="text/javascript">
  233. initUI();
  234. loadServers();
  235. </script>
  236. </body>
  237. </html>