index.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <!-- index.html -->
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>聊天室</title>
  7. </head>
  8. <body>
  9. <!-- 主页 -->
  10. <div id="home" style="display: block;">
  11. <input type="text" id="usernameInput" placeholder="昵称">
  12. <input type="text" id="roomInput" placeholder="聊天室ID">
  13. <button onclick="enterRoom()">进入</button>
  14. </div>
  15. <!-- 聊天室 -->
  16. <div id="chatroom" style="display: none;">
  17. <ul id="messageList"></ul>
  18. <input type="text" id="messageInput">
  19. <button onclick="sendMessage()">发送</button>
  20. <button onclick="leaveRoom()">退出</button>
  21. </div>
  22. <script>// chat.js
  23. const usernameInput = document.getElementById('usernameInput');
  24. const roomInput = document.getElementById('roomInput');
  25. const messageInput = document.getElementById('messageInput');
  26. const messageList = document.getElementById('messageList');
  27. function enterRoom() {
  28. const username = usernameInput.value || '匿名';
  29. const roomID = roomInput.value;
  30. fetch('/enter_room', {
  31. method: 'POST',
  32. headers: {
  33. 'Content-Type': 'application/x-www-form-urlencoded'
  34. },
  35. body: `username=${encodeURIComponent(username)}&room_id=${encodeURIComponent(roomID)}`
  36. })
  37. .then(response => response.text())
  38. .then(data => {
  39. console.log(data);
  40. // 隐藏主页,显示聊天室
  41. document.getElementById('home').style.display = 'none';
  42. document.getElementById('chatroom').style.display = 'block';
  43. })
  44. .catch(error => console.error('Error:', error));
  45. }
  46. function sendMessage() {
  47. const message = messageInput.value;
  48. const roomID = roomInput.value;
  49. fetch('/send_message', {
  50. method: 'POST',
  51. headers: {
  52. 'Content-Type': 'application/x-www-form-urlencoded'
  53. },
  54. body: `message=${encodeURIComponent(message)}&room_id=${encodeURIComponent(roomID)}`
  55. })
  56. .then(response => response.text())
  57. .then(data => {
  58. console.log(data);
  59. // 清空输入框
  60. messageInput.value = '';
  61. // 更新聊天记录
  62. updateMessages();
  63. })
  64. .catch(error => console.error('Error:', error));
  65. }
  66. function leaveRoom() {
  67. // 重置主页,隐藏聊天室
  68. document.getElementById('home').style.display = 'block';
  69. document.getElementById('chatroom').style.display = 'none';
  70. }
  71. function updateMessages() {
  72. const roomID = roomInput.value;
  73. fetch(`/get_messages?room_id=${encodeURIComponent(roomID)}`, {
  74. method: 'GET'
  75. })
  76. .then(response => response.json())
  77. .then(data => {
  78. // 清空当前消息列表
  79. messageList.innerHTML = '';
  80. // 添加新消息到列表
  81. data.messages.forEach(message => {
  82. const li = document.createElement('li');
  83. li.textContent = `${message.username}: ${message.message}`;
  84. messageList.appendChild(li);
  85. });
  86. })
  87. .catch(error => console.error('Error:', error));
  88. }</script>
  89. </body>
  90. </html>