ul.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. <h3>WebSocket Testing Interface</h3>
  18. <div class="ui form">
  19. <div class="field">
  20. <label>Recv</label>
  21. <textarea id="incoming"></textarea>
  22. </div>
  23. <div class="field">
  24. <label>Send</label>
  25. <input type="text" id="sendMsg">
  26. </div>
  27. <div class="field">
  28. <button class="ui blue button" onclick="sendws();">Send</button>
  29. </div>
  30. </div>
  31. <br>
  32. <button class="ui button" onclick="openws();">Open Connection</button>
  33. </div>
  34. <script>
  35. var ws;
  36. var nolog = false;
  37. $(window).ready(function() {
  38. $("#incoming").val("");
  39. });
  40. //Send WebSocket
  41. function sendws() {
  42. var value = $("#sendMsg").val();
  43. ws.send(value);
  44. log("✉️ " + value)
  45. $("#sendMsg").val("");
  46. }
  47. //Open WebSocket connection to test script
  48. function openws() {
  49. log("⏱️ Opening...");
  50. let socket = new WebSocket(getWSEndpoint() + "/system/ajgi/interface?script=Speedtest/special/wspeedtest.js");
  51. ws = socket;
  52. socket.onopen = function(e) {
  53. log("✔️ Opened");
  54. socket.send("UPL");
  55. uploadtest(socket);
  56. };
  57. socket.onmessage = function(event) {
  58. log(`✔️: ${event.data}`);
  59. };
  60. socket.onclose = function(event) {
  61. if (event.wasClean) {
  62. log(`📪 Connection Closed Cleanly code=${event.code} reason=${event.reason}`);
  63. } else {
  64. // e.g. server process killed or network down
  65. // event.code is usually 1006 in this case
  66. log(`❌ Connection Closed Unexpectedly`);
  67. }
  68. };
  69. socket.onerror = function(error) {
  70. log(`❌ ERROR! ${error.message}`);
  71. };
  72. }
  73. function log(content) {
  74. if (content.indexOf("DATA:") == -1) {
  75. $("#incoming").val($("#incoming").val() + content + "\n");
  76. $("#incoming").scrollTop($("#incoming")[0].scrollHeight);
  77. }
  78. }
  79. function getWSEndpoint() {
  80. //Open opeartion in websocket
  81. let protocol = "wss://";
  82. if (location.protocol !== 'https:') {
  83. protocol = "ws://";
  84. }
  85. wsControlEndpoint = (protocol + window.location.hostname + ":" + window.location.port);
  86. return wsControlEndpoint;
  87. }
  88. function uploadtest(socket) {
  89. log("OK!");
  90. var CurrentPow = 0;
  91. var CurrentDif = 0;
  92. var randomStr = rnd(1024);
  93. var filesize = "DATA:".length + randomStr.length;
  94. log("Generated");
  95. while (CurrentDif < 5) {
  96. var CurrentMB = Math.pow(2, CurrentPow);
  97. log("Current File Size:" + bytesToSize(CurrentMB));
  98. var start = new Date();
  99. //websocket.send("Start: " + start.toString());
  100. for (var i = 0; i < CurrentMB; i++) {
  101. socket.send("DATA:" + randomStr);
  102. }
  103. var end = new Date();
  104. //websocket.send("End: " + end.toString());
  105. CurrentDif = (end.getTime() - start.getTime()) / 1000;
  106. CurrentPow++;
  107. }
  108. socket.send("stop");
  109. log("Total transmitted:" + bytesToSize(CurrentMB * filesize));
  110. log("Total time:" + CurrentDif + "s");
  111. log("Bandwidth:" + bytesToSize(CurrentMB * filesize / CurrentDif) + "/s OR " + bytesToSpeed(CurrentMB * filesize / CurrentDif));
  112. }
  113. //https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript
  114. function rnd(length) {
  115. var result = '';
  116. var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  117. var charactersLength = characters.length;
  118. for (var i = 0; i < length; i++) {
  119. result += characters.charAt(Math.floor(Math.random() *
  120. charactersLength));
  121. }
  122. return result;
  123. }
  124. //https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
  125. function bytesToSize(bytes) {
  126. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  127. if (bytes == 0) return '0 Byte';
  128. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  129. return Math.round((bytes / Math.pow(1024, i)) * 100, 3) / 100 + ' ' + sizes[i];
  130. }
  131. function bytesToSpeed(bytes) {
  132. bytes = bytes * 8;
  133. var sizes = ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps'];
  134. if (bytes == 0) return '0 Byte';
  135. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1000)));
  136. return Math.round((bytes / Math.pow(1000, i)) * 100, 3) / 100 + ' ' + sizes[i];
  137. }
  138. </script>
  139. </body>
  140. </html>