| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <!DOCTYPE html>
- <html lang="zh-Hans">
- <head>
- <meta charset="UTF-8">
- <title>聊天室</title>
- <script>
- // Polyfill for IE9 and below to support addEventListener
- if (!Element.prototype.addEventListener) {
- Element.prototype.addEventListener = function (eventName, callback) {
- this.attachEvent('on' + eventName, callback);
- };
- }
- // Polyfill for IE9 and below to support removeEventListener
- if (!Element.prototype.removeEventListener) {
- Element.prototype.removeEventListener = function (eventName, callback) {
- this.detachEvent('on' + eventName, callback);
- };
- }
- </script>
- <link type="text/css" rel="stylesheet" href="html-chat.css">
- </head>
- <body>
- <div class="container">
- <form id="roomForm">
- <fieldset>
- <legend>主页</legend>
- <label for="nickname">昵称:</label>
- <input type="text" id="nickname" name="nickname" value="匿名">
- <br>
- <label for="roomId">房间号:</label>
- <input type="text" id="roomId" name="roomId">
- <br>
- <button type="submit">进入聊天室</button>
- </fieldset>
- </form>
- <div id="chatArea" style="display: none;">
- <fieldset>
- <legend>聊天室</legend>
- <textarea id="chatLog" rows="10" cols="50" readonly></textarea>
- <br>
- <input type="text" id="messageInput">
- <button id="sendButton">发送</button>
- <button id="exitButton">退出</button>
- </fieldset>
- </div>
- </div>
- <script>
- if (typeof window.fetch !== 'function') {
- require('whatwg-fetch');
- }
- var currentRoomId = null;
- var messageUpdateInterval;
- document.getElementById('roomForm').addEventListener('submit', function (event) {
- event.preventDefault();
- const nickname = document.getElementById('nickname').value;
- const roomId = document.getElementById('roomId').value;
- fetch('/join', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json; charset=utf-8' },
- body: JSON.stringify({ nickname, roomId })
- }).then(function (response) {
- if (response.ok) {
- document.getElementById('roomForm').style.display = 'none';
- document.getElementById('chatArea').style.display = 'block';
- loadMessages(roomId);
- currentRoomId = roomId;
- startMessageUpdates();
- }
- });
- });
- document.getElementById('sendButton').addEventListener('click', function () {
- const message = document.getElementById('messageInput').value;
- fetch('/send', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json; charset=utf-8' },
- body: JSON.stringify({ message, roomId: currentRoomId })
- }).then(loadMessages(currentRoomId));
- document.getElementById('messageInput').value = '';
- clearInterval(messageUpdateInterval);
- });
- document.getElementById('exitButton').addEventListener('click', function () {
- fetch('/leave', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json; charset=utf-8' },
- body: JSON.stringify({ roomId: currentRoomId })
- }).then(() => {
- document.getElementById('chatLog').value = '';
- document.getElementById('roomForm').style.display = 'block';
- document.getElementById('chatArea').style.display = 'none';
- });
- });
- function startMessageUpdates() {
- loadMessages(currentRoomId);
- messageUpdateInterval = setInterval(() => loadMessages(currentRoomId), 30000); // 30 秒
- }
- function loadMessages(roomId) {
- fetch(`/messages?roomId=${roomId}`).then(response => response.json()).then(updateChatLog);
- }
- function updateChatLog(messages) {
- const chatLog = document.getElementById('chatLog');
- chatLog.value = messages.map(msg => `${new Date(msg.timestamp).toLocaleString()}: ${msg.nickname} - ${msg.message}`).join('\n');
- }
- </script>
- </body>
- </html>
|