Quellcode durchsuchen

【更新】需求

PJ-568 vor 2 Jahren
Ursprung
Commit
79a87600ca
4 geänderte Dateien mit 265 neuen und 14 gelöschten Zeilen
  1. 1 1
      index.html
  2. 87 0
      new_chat.html
  3. 89 0
      new_index.html
  4. 88 13
      需求.md

+ 1 - 1
index.html

@@ -89,7 +89,7 @@
         <form id="roomForm">
             <fieldset>
                 <legend>主页</legend>
-                <label for="nickname">昵称</label>
+                <label for="nickname">昵称:</label>
                 <input type="text" id="nickname" name="nickname" value="匿名">
                 <br>
                 <label for="roomId">房间号:</label>

+ 87 - 0
new_chat.html

@@ -0,0 +1,87 @@
+<!DOCTYPE html>
+<html lang="zh-Hans">
+
+<head>
+    <meta charset="UTF-8">
+    <title>聊天室 - {{ roomid }}</title>
+    <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 action="/send_message" method="post">
+            <fieldset>
+                <legend>聊天室 - {{ roomid }}</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>
+        </form>
+    </div>
+</body>
+
+</html>

+ 89 - 0
new_index.html

@@ -0,0 +1,89 @@
+<!DOCTYPE html>
+<html lang="zh-Hans">
+
+<head>
+    <meta charset="UTF-8">
+    <title>聊天室</title>
+    <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 action="/chat" method="get">
+            <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" value="默认">
+                <br>
+                <button type="submit">进入聊天室</button>
+            </fieldset>
+        </form>
+    </div>
+</body>
+
+</html>

+ 88 - 13
需求.md

@@ -1,29 +1,104 @@
-前端使用服务器生产界面(尽量兼容老浏览器(NetSurf)且尽可能不使用JS)。
+统一 UTF-8 编码。
+前端使用 HTML(尽量兼容老旧浏览器(NetSurf)且不使用JS)。
 服务端使用 python(尽量不引入第三方库),搭建最小轻量化独立聊天室。慢慢来,实现以下功能:
 
 ## 主页
 
 地址:(/)
 
-1. 用户可设置昵称(若为空,默认为“匿名”)。
-2. 在主页输入号码进入或创建聊天室。每一个聊天室有一个唯一 id(即号码),如果存在则进入,不存在则创建。若号码为空,则默认为“default”。
+用户界面:
+
+```html
+<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>
+```
+
+1. 用户设置昵称,空值默认为“匿名”。
+2. 输入或创建聊天室ID,空值默认为“默认”。
 3. 只能在主页输入设置昵称和号码。
 
 ## 聊天室
 
 地址:(/chat?nickname=昵称&roomid=聊天室号码&message=发送消息内容)
 
-1. 聊天记录区,发送消息,退出键退出聊天室。
-2. 网页每隔 20 秒获取聊天记录。
+用户界面:
+
+```html index.html
+<form action="/chat" method="get">
+    <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" value="默认">
+        <br>
+        <button type="submit">进入聊天室</button>
+    </fieldset>
+</form>
+```
+
+```html chat.html
+<form action="/send_message" method="post">
+    <fieldset>
+        <legend>聊天室 - {{ roomid }}</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>
+</form>
+```
+
+1. 聊天记录区,发送消息,退出键返回主页。
+2. 客户端每20秒自动刷新聊天记录。
 3. 每一个聊天室都有独立的聊天记录存储。进入聊天室的用户能看到他人发出的消息。
-4. 每一条聊天记录不超过 2048 字节。
-5. 每分钟不能发送超过 20 条信息。
-6. 每个聊天室能报存 50 条聊天记录,如果超出则移除最早的聊天记录。
-7. 最多存在32个聊天室,如果超出则移除最早的聊天室。
+4. 单条消息不超过2048字节,每分钟不超过20条消息。
+5. 每个聊天室存储最多50条聊天记录,超出则删除最早记录。
+6. 总聊天室数量限制为32个,超出则删除最早的聊天室。
 
 ## 服务端
 
-- 服务器生成界面。
-- 输出 log。
-- 每隔120秒将聊天室数据以ini格式保存到指定位置(默认./chat_records.ini)。
-- 通过 argparse 传入设置(-p指定端口,-o指定聊天室数据保存文件),传入 `--help` 或 `-h` 查看所有设置信息。
+- 服务器动态生成聊天室界面。
+- 记录日志。
+- 每 120 秒以 ini 格式保存内存中的聊天室数据至指定位置(默认./chat_records.ini)。
+- 使用 argparse 处理命令行参数,包括端口、数据保存文件路径及帮助信息展示。
+
+### 服务端实现
+
+- 配置读写:
+  - 使用ConfigParser从chat_records.ini文件加载聊天室数据。
+在关闭或更新聊天室状态后,将数据保存回chat_records.ini。
+
+- HTTP请求处理:
+  - 继承自BaseHTTPRequestHandler类的ChatServer用于处理HTTP请求。
+do_POST方法处理POST请求,支持用户加入聊天室、发送消息和离开聊天室。
+do_GET方法处理GET请求,可以返回HTML页面或特定聊天室的消息列表。
+
+- 聊天室管理:
+  - 聊天室数据存储在ChatServer.rooms字典中,每个聊天室存储用户列表和消息列表。
+支持的最大房间数、每间房的消息数、消息长度和每分钟消息数有限制。
+当聊天室数量超过限制时,会删除最老的聊天室。
+消息列表达到上限时,自动移除最旧的消息。
+
+- 用户交互:
+  - 用户通过POST请求加入聊天室,发送消息或离开。
+加入聊天室时,用户昵称被记录;发送消息时,使用最新的用户昵称。
+每条消息包含时间戳、昵称和消息内容。
+
+- 安全与启动:
+  - 服务器可以通过HTTPS运行,需要server.crt和server.key文件。
+如果证书和密钥文件不存在,服务器将以HTTP模式运行。
+启动时,服务器监听指定端口,并在启动前加载聊天室数据。
+
+- 命令行参数:
+  - 使用argparse模块解析命令行参数,允许用户指定服务器监听的端口。