example-multipleServers-pretty.html 6.5 KB

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