index.html.bak 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. <link type="text/css" rel="stylesheet" href="html-chat.css">
  21. </head>
  22. <body>
  23. <div class="container">
  24. <form id="roomForm">
  25. <fieldset>
  26. <legend>主页</legend>
  27. <label for="nickname">昵称:</label>
  28. <input type="text" id="nickname" name="nickname" value="匿名">
  29. <br>
  30. <label for="roomId">房间号:</label>
  31. <input type="text" id="roomId" name="roomId">
  32. <br>
  33. <button type="submit">进入聊天室</button>
  34. </fieldset>
  35. </form>
  36. <div id="chatArea" style="display: none;">
  37. <fieldset>
  38. <legend>聊天室</legend>
  39. <textarea id="chatLog" rows="10" cols="50" readonly></textarea>
  40. <br>
  41. <input type="text" id="messageInput">
  42. <button id="sendButton">发送</button>
  43. <button id="exitButton">退出</button>
  44. </fieldset>
  45. </div>
  46. </div>
  47. <script>
  48. if (typeof window.fetch !== 'function') {
  49. require('whatwg-fetch');
  50. }
  51. var currentRoomId = null;
  52. var messageUpdateInterval;
  53. document.getElementById('roomForm').addEventListener('submit', function (event) {
  54. event.preventDefault();
  55. const nickname = document.getElementById('nickname').value;
  56. const roomId = document.getElementById('roomId').value;
  57. fetch('/join', {
  58. method: 'POST',
  59. headers: { 'Content-Type': 'application/json; charset=utf-8' },
  60. body: JSON.stringify({ nickname, roomId })
  61. }).then(function (response) {
  62. if (response.ok) {
  63. document.getElementById('roomForm').style.display = 'none';
  64. document.getElementById('chatArea').style.display = 'block';
  65. loadMessages(roomId);
  66. currentRoomId = roomId;
  67. startMessageUpdates();
  68. }
  69. });
  70. });
  71. document.getElementById('sendButton').addEventListener('click', function () {
  72. const message = document.getElementById('messageInput').value;
  73. fetch('/send', {
  74. method: 'POST',
  75. headers: { 'Content-Type': 'application/json; charset=utf-8' },
  76. body: JSON.stringify({ message, roomId: currentRoomId })
  77. }).then(loadMessages(currentRoomId));
  78. document.getElementById('messageInput').value = '';
  79. clearInterval(messageUpdateInterval);
  80. });
  81. document.getElementById('exitButton').addEventListener('click', function () {
  82. fetch('/leave', {
  83. method: 'POST',
  84. headers: { 'Content-Type': 'application/json; charset=utf-8' },
  85. body: JSON.stringify({ roomId: currentRoomId })
  86. }).then(() => {
  87. document.getElementById('chatLog').value = '';
  88. document.getElementById('roomForm').style.display = 'block';
  89. document.getElementById('chatArea').style.display = 'none';
  90. });
  91. });
  92. function startMessageUpdates() {
  93. loadMessages(currentRoomId);
  94. messageUpdateInterval = setInterval(() => loadMessages(currentRoomId), 30000); // 30 秒
  95. }
  96. function loadMessages(roomId) {
  97. fetch(`/messages?roomId=${roomId}`).then(response => response.json()).then(updateChatLog);
  98. }
  99. function updateChatLog(messages) {
  100. const chatLog = document.getElementById('chatLog');
  101. chatLog.value = messages.map(msg => `${new Date(msg.timestamp).toLocaleString()}: ${msg.nickname} - ${msg.message}`).join('\n');
  102. }
  103. </script>
  104. </body>
  105. </html>