| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <!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>
- <style>
- body {
- font-family: Arial, sans-serif;
- background-color: #f4f4f4;
- margin: 0;
- padding: 0;
- color: #333;
- }
- .container {
- width: 80%;
- max-width: 600px;
- margin: 20px auto;
- padding: 20px;
- background-color: #fff;
- border: 1px solid #ccc;
- box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1);
- border-radius: 5px; /* IE9+ */
- }
- fieldset {
- border: 1px solid #ddd;
- padding: 10px;
- margin-bottom: 5px;
- }
- legend {
- font-weight: bold;
- padding: 0 10px;
- }
- label {
- display: block;
- margin-bottom: 5px;
- }
- input[type="text"],
- textarea {
- box-sizing: border-box;
- max-width: 100%;
- width: 100%;
- padding: 8px;
- margin-bottom: 10px;
- border: 1px solid #ddd;
- border-radius: 3px; /* IE9+ */
- }
- button {
- padding: 8px 15px;
- background-color: #007BFF;
- color: #fff;
- border: none;
- border-radius: 3px; /* IE9+ */
- cursor: pointer;
- }
- button:hover {
- background-color: #0056b3;
- }
- </style>
- </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>
- let currentRoomId = null;
- 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' },
- body: JSON.stringify({ nickname, roomId })
- }).then(response => {
- if (response.ok) {
- document.getElementById('roomForm').style.display = 'none';
- document.getElementById('chatArea').style.display = 'block';
- loadMessages(roomId);
- currentRoomId = roomId;
- }
- });
- });
- document.getElementById('sendButton').addEventListener('click', function () {
- const message = document.getElementById('messageInput').value;
- fetch('/send', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ message, roomId: currentRoomId })
- }).then(loadMessages(currentRoomId));
- });
- document.getElementById('exitButton').addEventListener('click', function () {
- fetch('/leave', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ roomId: currentRoomId })
- }).then(() => {
- document.getElementById('roomForm').style.display = 'block';
- document.getElementById('chatArea').style.display = 'none';
- });
- });
- 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>
|