index.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title></title>
  6. <style>
  7. table { border: 0 }
  8. .commslog-data { font-family: Consolas, Courier New, Courier, monospace; }
  9. .commslog-server { background-color: red; color: white }
  10. .commslog-client { background-color: green; color: white }
  11. </style>
  12. </head>
  13. <body>
  14. <h1>WebSocket Sample Application</h1>
  15. <p id="stateLabel">Ready to connect...</p>
  16. <div>
  17. <label for="connectionUrl">WebSocket Server URL:</label>
  18. <input id="connectionUrl" />
  19. <button id="connectButton" type="submit">Connect</button>
  20. </div>
  21. <p></p>
  22. <div>
  23. <label for="sendMessage">Message to send:</label>
  24. <input id="sendMessage" disabled />
  25. <button id="sendButton" type="submit" disabled>Send</button>
  26. <button id="closeButton" disabled>Close Socket</button>
  27. </div>
  28. <h2>Communication Log</h2>
  29. <table style="width: 800px">
  30. <thead>
  31. <tr>
  32. <td style="width: 100px">From</td>
  33. <td style="width: 100px">To</td>
  34. <td>Data</td>
  35. </tr>
  36. </thead>
  37. <tbody id="commsLog">
  38. </tbody>
  39. </table>
  40. <script>
  41. var connectionUrl = document.getElementById("connectionUrl");
  42. var connectButton = document.getElementById("connectButton");
  43. var stateLabel = document.getElementById("stateLabel");
  44. var sendMessage = document.getElementById("sendMessage");
  45. var sendButton = document.getElementById("sendButton");
  46. var commsLog = document.getElementById("commsLog");
  47. var closeButton = document.getElementById("closeButton");
  48. var socket;
  49. var scheme = document.location.protocol === "https:" ? "wss" : "ws";
  50. var port = document.location.port ? (":" + document.location.port) : "";
  51. connectionUrl.value = scheme + "://game.bubblesocket"
  52. function updateState() {
  53. function disable() {
  54. sendMessage.disabled = true;
  55. sendButton.disabled = true;
  56. closeButton.disabled = true;
  57. }
  58. function enable() {
  59. sendMessage.disabled = false;
  60. sendButton.disabled = false;
  61. closeButton.disabled = false;
  62. }
  63. connectionUrl.disabled = true;
  64. connectButton.disabled = true;
  65. if (!socket) {
  66. disable();
  67. } else {
  68. switch (socket.readyState) {
  69. case WebSocket.CLOSED:
  70. stateLabel.innerHTML = "Closed";
  71. disable();
  72. connectionUrl.disabled = false;
  73. connectButton.disabled = false;
  74. break;
  75. case WebSocket.CLOSING:
  76. stateLabel.innerHTML = "Closing...";
  77. disable();
  78. break;
  79. case WebSocket.CONNECTING:
  80. stateLabel.innerHTML = "Connecting...";
  81. disable();
  82. break;
  83. case WebSocket.OPEN:
  84. stateLabel.innerHTML = "Open";
  85. enable();
  86. break;
  87. default:
  88. stateLabel.innerHTML = "Unknown WebSocket State: " + htmlEscape(socket.readyState);
  89. disable();
  90. break;
  91. }
  92. }
  93. }
  94. closeButton.onclick = function () {
  95. if (!socket || socket.readyState !== WebSocket.OPEN) {
  96. alert("socket not connected");
  97. }
  98. socket.close(1000, "Closing from client");
  99. };
  100. sendButton.onclick = function () {
  101. if (!socket || socket.readyState !== WebSocket.OPEN) {
  102. alert("socket not connected");
  103. }
  104. var data = sendMessage.value;
  105. socket.send(data);
  106. commsLog.innerHTML += '<tr>' +
  107. '<td class="commslog-client">Client</td>' +
  108. '<td class="commslog-server">Server</td>' +
  109. '<td class="commslog-data">' + htmlEscape(data) + '</td></tr>';
  110. };
  111. connectButton.onclick = function() {
  112. stateLabel.innerHTML = "Connecting";
  113. socket = new WebSocket(connectionUrl.value);
  114. socket.onopen = function (event) {
  115. updateState();
  116. commsLog.innerHTML += '<tr>' +
  117. '<td colspan="3" class="commslog-data">Connection opened</td>' +
  118. '</tr>';
  119. };
  120. socket.onclose = function (event) {
  121. updateState();
  122. commsLog.innerHTML += '<tr>' +
  123. '<td colspan="3" class="commslog-data">Connection closed. Code: ' + htmlEscape(event.code) + '. Reason: ' + htmlEscape(event.reason) + '</td>' +
  124. '</tr>';
  125. };
  126. socket.onerror = updateState;
  127. socket.onmessage = function (event) {
  128. event.data.text().then((textData) => {
  129. commsLog.innerHTML += '<tr>' +
  130. '<td class="commslog-server">Server</td>' +
  131. '<td class="commslog-client">Client</td>' +
  132. '<td class="commslog-data">' + htmlEscape(textData) + '</td></tr>';
  133. })
  134. };
  135. };
  136. function htmlEscape(str) {
  137. console.log(str)
  138. return str.toString()
  139. .replace(/&/g, '&amp;')
  140. .replace(/"/g, '&quot;')
  141. .replace(/'/g, '&#39;')
  142. .replace(/</g, '&lt;')
  143. .replace(/>/g, '&gt;');
  144. }
  145. </script>
  146. </body>
  147. </html>