index.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="apple-mobile-web-app-capable" content="yes" />
  5. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1" />
  6. <meta charset="UTF-8">
  7. <meta name="theme-color" content="#4b75ff">
  8. <link rel="stylesheet" href="../script/semantic/semantic.min.css">
  9. <script src="../script/jquery.min.js"></script>
  10. <script src="../script/ao_module.js"></script>
  11. <script src="../script/semantic/semantic.min.js"></script>
  12. <title>WebSocket Test</title>
  13. </head>
  14. <body>
  15. <br><br>
  16. <div class="ui container">
  17. <div class="ui three statistics">
  18. <div class="statistic">
  19. <div class="value" id="dl" style="padding: 5px;">
  20. -
  21. </div>
  22. <div class="label" id="dll">
  23. Download
  24. </div>
  25. </div>
  26. <div class="statistic">
  27. <div class="value" id="ul" style="padding: 5px;">
  28. -
  29. </div>
  30. <div class="label" id="ull">
  31. Upload
  32. </div>
  33. </div>
  34. <div class="statistic">
  35. <div class="value" id="ping" style="padding: 5px;">
  36. -
  37. </div>
  38. <div class="label" id="pingl">
  39. Ping
  40. </div>
  41. </div>
  42. </div>
  43. <br>
  44. <div>
  45. <div class="ui top attached teal progress" id="progressbar" style="display: none">
  46. <div class="bar">
  47. <div class="progress"></div>
  48. </div>
  49. </div>
  50. <button class="ui fluid button" onclick="startBench();" id="startbtn">Test</button>
  51. </div>
  52. </div>
  53. </body>
  54. <script>
  55. ao_module_setFixedWindowSize();
  56. $('#progressbar').progress({
  57. label: 'ratio',
  58. text: {
  59. ratio: ''
  60. },
  61. total: 100,
  62. percent: 0
  63. });
  64. function startBench() {
  65. $('#progressbar').removeAttr('style');
  66. $('#progressbar').progress('set progress', 0);
  67. $("#startbtn").attr('disabled', 'disabled');
  68. $("#startbtn").html('Testing...');
  69. downloadBench();
  70. }
  71. function downloadBench() {
  72. $("#dl").text("⏱️");
  73. let socket = new WebSocket(getWSEndpoint() + "/system/ajgi/interface?script=Speedtest/special/wspeedtest.js");
  74. socket.onopen = function(e) {
  75. $("#dl").text("⌛");
  76. socket.send("DWL");
  77. $('#progressbar').progress('set progress', 0);
  78. };
  79. socket.onmessage = function(event) {
  80. if (event.data.indexOf("DATA:") == -1) {
  81. if (event.data.indexOf("TTL_BANDWIDTH") != -1) {
  82. let bandwidth = event.data.split("=")[1].split(" ");
  83. $("#dl").text(bandwidth[0]);
  84. $("#dll").text("Download (" + bandwidth[1] + ")");
  85. uploadBench();
  86. }
  87. if (event.data.indexOf("TIME_DIFF") != -1) {
  88. let timediff = parseInt(event.data.split("=")[1]);
  89. $('#progressbar').progress('increment', timediff / 5 * 100);
  90. }
  91. }
  92. };
  93. socket.onerror = function(error) {
  94. if ($("#dl").text() != "⌛") {
  95. $("#dl").text("❌");
  96. }
  97. };
  98. }
  99. function uploadBench() {
  100. $("#ul").text("⏱️");
  101. let socket = new WebSocket(getWSEndpoint() + "/system/ajgi/interface?script=Speedtest/special/wspeedtest.js");
  102. socket.onopen = function(e) {
  103. $("#ul").text("⌛");
  104. socket.send("UPL");
  105. setTimeout(executeULTest, 1000, socket);
  106. };
  107. socket.onerror = function(error) {
  108. if ($("#ul").text() != "⌛") {
  109. $("#ul").text("❌");
  110. }
  111. };
  112. }
  113. function executeULTest(socket) {
  114. $('#progressbar').progress('set progress', 0);
  115. let CurrentMB = 0;
  116. let start = new Date();
  117. while (socket.bufferedAmount < 10485760 * 10) {
  118. socket.send(new ArrayBuffer(32768));
  119. CurrentMB += 32768;
  120. }
  121. let checker = setInterval(function() {
  122. if (socket.bufferedAmount == 0) {
  123. let end = new Date();
  124. CurrentDif = (end.getTime() - start.getTime()) / 1000;
  125. socket.send("stop");
  126. let bandwidth = bytesToSpeed(CurrentMB / CurrentDif).split(" ");
  127. $("#ul").text(bandwidth[0]);
  128. $("#ull").text("Upload (" + bandwidth[1] + ")");
  129. $('#progressbar').progress('set progress', 0);
  130. clearInterval(checker);
  131. pingTest();
  132. } else {
  133. //(1 - (socket.bufferedAmount / (10485760 * 10))) * 100
  134. $('#progressbar').progress('set progress', 100 - (socket.bufferedAmount / 1048576));
  135. }
  136. }, 100);
  137. }
  138. function pingTest() {
  139. $('#progressbar').progress('set progress', 0);
  140. let clientSendTimeStamp = [];
  141. let serverResponseTimeStamp = [];
  142. let clientReceiveTimeStamp = [];
  143. $("#ping").text("⏱️");
  144. let socket = new WebSocket(getWSEndpoint() + "/system/ajgi/interface?script=Speedtest/special/wspeedtest.js");
  145. socket.onopen = function(e) {
  146. $("#ping").text("⌛");
  147. socket.send("PING");
  148. setTimeout(executePingTest, 500, socket, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp, 0);
  149. };
  150. socket.onmessage = function(event) {
  151. clientReceiveTimeStamp.push(new Date().getTime());
  152. serverResponseTimeStamp.push(event.data);
  153. if (clientReceiveTimeStamp.length == 5) {
  154. setTimeout(showPingResult, 2000, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp);
  155. }
  156. $('#progressbar').progress('increment', 16.67);
  157. };
  158. socket.onerror = function(error) {
  159. if ($("#ping").text() != "⌛") {
  160. $("#ping").text("❌");
  161. }
  162. };
  163. }
  164. function executePingTest(socket, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp, count) {
  165. let clientTime = new Date();
  166. socket.send(clientTime);
  167. clientSendTimeStamp.push(clientTime.getTime());
  168. count++;
  169. $('#progressbar').progress('increment', 16.67);
  170. if (count < 3) {
  171. setTimeout(executePingTest, 1000, socket, clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp, count);
  172. }
  173. }
  174. function showPingResult(clientSendTimeStamp, serverResponseTimeStamp, clientReceiveTimeStamp) {
  175. serverResponseTimeStamp.shift();
  176. serverResponseTimeStamp.shift();
  177. clientReceiveTimeStamp.shift();
  178. clientReceiveTimeStamp.shift();
  179. let diff = 0;
  180. for (let i = 0; i < clientSendTimeStamp.length; i++) {
  181. diff += (clientReceiveTimeStamp[i] - clientSendTimeStamp[i]) - (serverResponseTimeStamp[i].split(",")[1] - serverResponseTimeStamp[i].split(",")[0]);
  182. }
  183. $("#ping").text(Math.round((diff / clientSendTimeStamp.length) * 100) / 100);
  184. $("#pingl").text("Ping (ms)");
  185. //restart the test
  186. $('#progressbar').attr('style', 'display: none');
  187. $("#startbtn").html('Re-Test');
  188. $("#startbtn").attr('onclick', 'location.reload();');
  189. $("#startbtn").removeAttr('disabled');
  190. }
  191. function getWSEndpoint() {
  192. //Open opeartion in websocket
  193. let protocol = "wss://";
  194. if (location.protocol !== 'https:') {
  195. protocol = "ws://";
  196. }
  197. wsControlEndpoint = (protocol + window.location.hostname + ":" + window.location.port);
  198. return wsControlEndpoint;
  199. }
  200. //https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
  201. function bytesToSize(bytes) {
  202. let sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  203. if (bytes == 0) return '0 Byte';
  204. let i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  205. return Math.round((bytes / Math.pow(1024, i)) * 100, 3) / 100 + ' ' + sizes[i];
  206. }
  207. function bytesToSpeed(bytes) {
  208. bytes = bytes * 8;
  209. let sizes = ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps'];
  210. if (bytes == 0) return '0 Byte';
  211. let i = parseInt(Math.floor(Math.log(bytes) / Math.log(1000)));
  212. return Math.round((bytes / Math.pow(1000, i)) * 100, 3) / 100 + ' ' + sizes[i];
  213. }
  214. </script>
  215. </html>