example-multipleServers-pretty.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. border-radius:0.3em;
  110. transition:all 0.3s;
  111. box-sizing:border-box;
  112. width:8em; height:3em;
  113. line-height:2.7em;
  114. cursor:pointer;
  115. box-shadow: 0 0 0 rgba(0,0,0,0.1), inset 0 0 0 rgba(0,0,0,0.1);
  116. }
  117. #startStopBtn:hover{
  118. box-shadow: 0 0 2em rgba(0,0,0,0.1), inset 0 0 1em rgba(0,0,0,0.1);
  119. }
  120. #startStopBtn.running{
  121. background-color:#FF3030;
  122. border-color:#FF6060;
  123. color:#FFFFFF;
  124. }
  125. #startStopBtn:before{
  126. content:"Start";
  127. }
  128. #startStopBtn.running:before{
  129. content:"Abort";
  130. }
  131. #test{
  132. margin-top:2em;
  133. margin-bottom:12em;
  134. }
  135. div.testArea{
  136. display:inline-block;
  137. width:14em;
  138. height:9em;
  139. position:relative;
  140. box-sizing:border-box;
  141. }
  142. div.testName{
  143. position:absolute;
  144. top:0.1em; left:0;
  145. width:100%;
  146. font-size:1.4em;
  147. z-index:9;
  148. }
  149. div.meterText{
  150. position:absolute;
  151. bottom:1.5em; left:0;
  152. width:100%;
  153. font-size:2.5em;
  154. z-index:9;
  155. }
  156. #dlText{
  157. color:#6060AA;
  158. }
  159. #ulText{
  160. color:#309030;
  161. }
  162. #pingText,#jitText{
  163. color:#AA6060;
  164. }
  165. div.meterText:empty:before{
  166. color:#505050 !important;
  167. content:"0.00";
  168. }
  169. div.unit{
  170. position:absolute;
  171. bottom:2em; left:0;
  172. width:100%;
  173. z-index:9;
  174. }
  175. div.testGroup{
  176. display:inline-block;
  177. }
  178. @media all and (max-width:65em){
  179. body{
  180. font-size:1.5vw;
  181. }
  182. }
  183. @media all and (max-width:40em){
  184. body{
  185. font-size:0.8em;
  186. }
  187. div.testGroup{
  188. display:block;
  189. margin: 0 auto;
  190. }
  191. }
  192. </style>
  193. </head>
  194. <body>
  195. <h1>LibreSpeed Example</h1>
  196. <div id="startStopBtn" onclick="startStop()"></div>
  197. <div id="serverId">Selecting server...</div>
  198. <div id="test">
  199. <div class="testGroup">
  200. <div class="testArea">
  201. <div class="testName">Download</div>
  202. <div id="dlText" class="meterText"></div>
  203. <div class="unit">Mbit/s</div>
  204. </div>
  205. <div class="testArea">
  206. <div class="testName">Upload</div>
  207. <div id="ulText" class="meterText"></div>
  208. <div class="unit">Mbit/s</div>
  209. </div>
  210. </div>
  211. <div class="testGroup">
  212. <div class="testArea">
  213. <div class="testName">Ping</div>
  214. <div id="pingText" class="meterText"></div>
  215. <div class="unit">ms</div>
  216. </div>
  217. <div class="testArea">
  218. <div class="testName">Jitter</div>
  219. <div id="jitText" class="meterText"></div>
  220. <div class="unit">ms</div>
  221. </div>
  222. </div>
  223. <div id="ipArea">
  224. IP Address: <span id="ip"></span>
  225. </div>
  226. </div>
  227. <a href="https://github.com/librespeed/speedtest">Source code</a>
  228. <script type="text/javascript">
  229. initUI();
  230. loadServers();
  231. </script>
  232. </body>
  233. </html>