index.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <!DOCTYPE html>
  2. <html lang="zh-Hans">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>聊天室</title>
  6. <script>
  7. // Polyfill for IE9 and below to support addEventListener
  8. if (!Element.prototype.addEventListener) {
  9. Element.prototype.addEventListener = function (eventName, callback) {
  10. this.attachEvent('on' + eventName, callback);
  11. };
  12. }
  13. // Polyfill for IE9 and below to support removeEventListener
  14. if (!Element.prototype.removeEventListener) {
  15. Element.prototype.removeEventListener = function (eventName, callback) {
  16. this.detachEvent('on' + eventName, callback);
  17. };
  18. }
  19. </script>
  20. <style>
  21. body {
  22. font-family: Arial, sans-serif;
  23. background-color: #f4f4f4;
  24. margin: 0;
  25. padding: 0;
  26. color: #333;
  27. }
  28. .container {
  29. width: 80%;
  30. max-width: 600px;
  31. margin: 20px auto;
  32. padding: 20px;
  33. background-color: #fff;
  34. border: 1px solid #ccc;
  35. box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1);
  36. border-radius: 5px;
  37. /* IE9+ */
  38. }
  39. fieldset {
  40. border: 1px solid #ddd;
  41. padding: 10px;
  42. margin-bottom: 5px;
  43. }
  44. legend {
  45. font-weight: bold;
  46. padding: 0 10px;
  47. }
  48. label {
  49. display: block;
  50. margin-bottom: 5px;
  51. }
  52. input[type="text"],
  53. textarea {
  54. box-sizing: border-box;
  55. max-width: 100%;
  56. width: 100%;
  57. padding: 8px;
  58. margin-bottom: 10px;
  59. border: 1px solid #ddd;
  60. border-radius: 3px;
  61. /* IE9+ */
  62. }
  63. button {
  64. padding: 8px 15px;
  65. background-color: #007BFF;
  66. color: #fff;
  67. border: none;
  68. border-radius: 3px;
  69. /* IE9+ */
  70. cursor: pointer;
  71. }
  72. button:hover {
  73. background-color: #0056b3;
  74. }
  75. </style>
  76. </head>
  77. <body>
  78. <div class="container">
  79. <form id="roomForm">
  80. <fieldset>
  81. <legend>主页</legend>
  82. <label for="nickname">昵称:</label>
  83. <input type="text" id="nickname" name="nickname" value="匿名">
  84. <br>
  85. <label for="roomId">房间号:</label>
  86. <input type="text" id="roomId" name="roomId">
  87. <br>
  88. <button type="submit">进入聊天室</button>
  89. </fieldset>
  90. </form>
  91. <div id="chatArea" style="display: none;">
  92. <fieldset>
  93. <legend>聊天室</legend>
  94. <textarea id="chatLog" rows="10" cols="50" readonly></textarea>
  95. <br>
  96. <input type="text" id="messageInput">
  97. <button id="sendButton">发送</button>
  98. <button id="exitButton">退出</button>
  99. </fieldset>
  100. </div>
  101. </div>
  102. <script>
  103. if (typeof window.fetch !== 'function') {
  104. require('whatwg-fetch');
  105. }
  106. var currentRoomId = null;
  107. var messageUpdateInterval;
  108. document.getElementById('roomForm').addEventListener('submit', function (event) {
  109. event.preventDefault();
  110. const nickname = document.getElementById('nickname').value;
  111. const roomId = document.getElementById('roomId').value;
  112. fetch('/join', {
  113. method: 'POST',
  114. headers: { 'Content-Type': 'application/json; charset=utf-8' },
  115. body: JSON.stringify({ nickname, roomId })
  116. }).then(function (response) {
  117. if (response.ok) {
  118. document.getElementById('roomForm').style.display = 'none';
  119. document.getElementById('chatArea').style.display = 'block';
  120. loadMessages(roomId);
  121. currentRoomId = roomId;
  122. startMessageUpdates();
  123. }
  124. });
  125. });
  126. document.getElementById('sendButton').addEventListener('click', function () {
  127. const message = document.getElementById('messageInput').value;
  128. fetch('/send', {
  129. method: 'POST',
  130. headers: { 'Content-Type': 'application/json; charset=utf-8' },
  131. body: JSON.stringify({ message, roomId: currentRoomId })
  132. }).then(loadMessages(currentRoomId));
  133. document.getElementById('messageInput').value = '';
  134. clearInterval(messageUpdateInterval);
  135. });
  136. document.getElementById('exitButton').addEventListener('click', function () {
  137. fetch('/leave', {
  138. method: 'POST',
  139. headers: { 'Content-Type': 'application/json; charset=utf-8' },
  140. body: JSON.stringify({ roomId: currentRoomId })
  141. }).then(() => {
  142. document.getElementById('chatLog').value = '';
  143. document.getElementById('roomForm').style.display = 'block';
  144. document.getElementById('chatArea').style.display = 'none';
  145. });
  146. });
  147. function startMessageUpdates() {
  148. loadMessages(currentRoomId);
  149. messageUpdateInterval = setInterval(() => loadMessages(currentRoomId), 30000); // 30 秒
  150. }
  151. function loadMessages(roomId) {
  152. fetch(`/messages?roomId=${roomId}`).then(response => response.json()).then(updateChatLog);
  153. }
  154. function updateChatLog(messages) {
  155. const chatLog = document.getElementById('chatLog');
  156. chatLog.value = messages.map(msg => `${new Date(msg.timestamp).toLocaleString()}: ${msg.nickname} - ${msg.message}`).join('\n');
  157. }
  158. </script>
  159. </body>
  160. </html>