1
0

index.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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; /* IE9+ */
  37. }
  38. fieldset {
  39. border: 1px solid #ddd;
  40. padding: 10px;
  41. margin-bottom: 5px;
  42. }
  43. legend {
  44. font-weight: bold;
  45. padding: 0 10px;
  46. }
  47. label {
  48. display: block;
  49. margin-bottom: 5px;
  50. }
  51. input[type="text"],
  52. textarea {
  53. box-sizing: border-box;
  54. max-width: 100%;
  55. width: 100%;
  56. padding: 8px;
  57. margin-bottom: 10px;
  58. border: 1px solid #ddd;
  59. border-radius: 3px; /* IE9+ */
  60. }
  61. button {
  62. padding: 8px 15px;
  63. background-color: #007BFF;
  64. color: #fff;
  65. border: none;
  66. border-radius: 3px; /* IE9+ */
  67. cursor: pointer;
  68. }
  69. button:hover {
  70. background-color: #0056b3;
  71. }
  72. </style>
  73. </head>
  74. <body>
  75. <div class="container">
  76. <form id="roomForm">
  77. <fieldset>
  78. <legend>主页</legend>
  79. <label for="nickname">昵称</label>
  80. <input type="text" id="nickname" name="nickname" value="匿名">
  81. <br>
  82. <label for="roomId">房间号:</label>
  83. <input type="text" id="roomId" name="roomId">
  84. <br>
  85. <button type="submit">进入聊天室</button>
  86. </fieldset>
  87. </form>
  88. <div id="chatArea" style="display: none;">
  89. <fieldset>
  90. <legend>聊天室</legend>
  91. <textarea id="chatLog" rows="10" cols="50" readonly></textarea>
  92. <br>
  93. <input type="text" id="messageInput">
  94. <button id="sendButton">发送</button>
  95. <button id="exitButton">退出</button>
  96. </fieldset>
  97. </div>
  98. </div>
  99. <script>
  100. let currentRoomId = null;
  101. document.getElementById('roomForm').addEventListener('submit', function (event) {
  102. event.preventDefault();
  103. const nickname = document.getElementById('nickname').value;
  104. const roomId = document.getElementById('roomId').value;
  105. fetch('/join', {
  106. method: 'POST',
  107. headers: { 'Content-Type': 'application/json' },
  108. body: JSON.stringify({ nickname, roomId })
  109. }).then(response => {
  110. if (response.ok) {
  111. document.getElementById('roomForm').style.display = 'none';
  112. document.getElementById('chatArea').style.display = 'block';
  113. loadMessages(roomId);
  114. currentRoomId = roomId;
  115. }
  116. });
  117. });
  118. document.getElementById('sendButton').addEventListener('click', function () {
  119. const message = document.getElementById('messageInput').value;
  120. fetch('/send', {
  121. method: 'POST',
  122. headers: { 'Content-Type': 'application/json' },
  123. body: JSON.stringify({ message, roomId: currentRoomId })
  124. }).then(loadMessages(currentRoomId));
  125. });
  126. document.getElementById('exitButton').addEventListener('click', function () {
  127. fetch('/leave', {
  128. method: 'POST',
  129. headers: { 'Content-Type': 'application/json' },
  130. body: JSON.stringify({ roomId: currentRoomId })
  131. }).then(() => {
  132. document.getElementById('roomForm').style.display = 'block';
  133. document.getElementById('chatArea').style.display = 'none';
  134. });
  135. });
  136. function loadMessages(roomId) {
  137. fetch(`/messages?roomId=${roomId}`).then(response => response.json()).then(updateChatLog);
  138. }
  139. function updateChatLog(messages) {
  140. const chatLog = document.getElementById('chatLog');
  141. chatLog.value = messages.map(msg => `${new Date(msg.timestamp).toLocaleString()}: ${msg.nickname} - ${msg.message}`).join('\n');
  142. }
  143. </script>
  144. </body>
  145. </html>