home.html 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Chat Example</title>
  5. <script type="text/javascript">
  6. window.onload = function () {
  7. var conn;
  8. var msg = document.getElementById("msg");
  9. var log = document.getElementById("log");
  10. function appendLog(item) {
  11. var doScroll = log.scrollTop > log.scrollHeight - log.clientHeight - 1;
  12. log.appendChild(item);
  13. if (doScroll) {
  14. log.scrollTop = log.scrollHeight - log.clientHeight;
  15. }
  16. }
  17. document.getElementById("form").onsubmit = function () {
  18. if (!conn) {
  19. return false;
  20. }
  21. if (!msg.value) {
  22. return false;
  23. }
  24. conn.send(msg.value);
  25. msg.value = "";
  26. return false;
  27. };
  28. if (window["WebSocket"]) {
  29. conn = new WebSocket("ws://" + document.location.host + "/ws");
  30. conn.onclose = function (evt) {
  31. var item = document.createElement("div");
  32. item.innerHTML = "<b>Connection closed.</b>";
  33. appendLog(item);
  34. };
  35. conn.onmessage = function (evt) {
  36. var messages = evt.data.split('\n');
  37. for (var i = 0; i < messages.length; i++) {
  38. var item = document.createElement("div");
  39. item.innerText = messages[i];
  40. appendLog(item);
  41. }
  42. };
  43. } else {
  44. var item = document.createElement("div");
  45. item.innerHTML = "<b>Your browser does not support WebSockets.</b>";
  46. appendLog(item);
  47. }
  48. };
  49. </script>
  50. <style type="text/css">
  51. html {
  52. overflow: hidden;
  53. }
  54. body {
  55. overflow: hidden;
  56. padding: 0;
  57. margin: 0;
  58. width: 100%;
  59. height: 100%;
  60. background: gray;
  61. }
  62. #log {
  63. background: white;
  64. margin: 0;
  65. padding: 0.5em 0.5em 0.5em 0.5em;
  66. position: absolute;
  67. top: 0.5em;
  68. left: 0.5em;
  69. right: 0.5em;
  70. bottom: 3em;
  71. overflow: auto;
  72. }
  73. #form {
  74. padding: 0 0.5em 0 0.5em;
  75. margin: 0;
  76. position: absolute;
  77. bottom: 1em;
  78. left: 0px;
  79. width: 100%;
  80. overflow: hidden;
  81. }
  82. </style>
  83. </head>
  84. <body>
  85. <div id="log"></div>
  86. <form id="form">
  87. <input type="submit" value="Send" />
  88. <input type="text" id="msg" size="64" autofocus />
  89. </form>
  90. </body>
  91. </html>