test.html 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <html>
  2. <head>
  3. <title>Simple client</title>
  4. <script type="text/javascript">
  5. var ws;
  6. function init() {
  7. // Connect to Web Socket
  8. ws = new WebSocket("ws://localhost:9001/");
  9. // Set event handlers.
  10. ws.onopen = function() {
  11. output("onopen");
  12. };
  13. ws.onmessage = function(e) {
  14. // e.data contains received string.
  15. output("onmessage: " + e.data);
  16. };
  17. ws.onclose = function() {
  18. output("onclose");
  19. };
  20. ws.onerror = function(e) {
  21. output("onerror");
  22. console.log(e)
  23. };
  24. }
  25. function onSubmit() {
  26. var input = document.getElementById("input");
  27. // You can send message to the Web Socket using ws.send.
  28. ws.send(input.value);
  29. output("send: " + input.value);
  30. input.value = "";
  31. input.focus();
  32. }
  33. function onCloseClick() {
  34. ws.close();
  35. }
  36. function output(str) {
  37. var log = document.getElementById("log");
  38. var escaped = str.replace(/&/, "&amp;").replace(/</, "&lt;").
  39. replace(/>/, "&gt;").replace(/"/, "&quot;"); // "
  40. log.innerHTML = escaped + "<br>" + log.innerHTML;
  41. }
  42. </script>
  43. </head>
  44. <body onload="init();">
  45. <form onsubmit="onSubmit(); return false;">
  46. <input type="text" id="input">
  47. <input type="submit" value="Send">
  48. <button onclick="onCloseClick(); return false;">close</button>
  49. </form>
  50. <div id="log"></div>
  51. </body>
  52. </html>