From fd25d57be56900f0e02709d1a4e641837c3e3ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BB=AA=E5=B1=B1=E4=B8=83=E5=AF=BB?= Date: Sun, 15 Jun 2025 15:05:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20API.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API.md | 159 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 API.md diff --git a/API.md b/API.md new file mode 100644 index 0000000..0c65658 --- /dev/null +++ b/API.md @@ -0,0 +1,159 @@ +## Socket API +Socket服务器运行在`52006`端口。客户端连接后,可以发送JSON格式的消息。每个消息必须包含`type`字段,表示操作类型。 + +### 消息格式 +所有消息均为JSON格式,编码为UTF-8字符串发送。 + +### 1. 注册 +- **请求**: +```json +{ +"type": "register", +"username": string, +"password": string, +} +``` + +- **响应**: +- 成功: +```json +{ +"type": "register", +"status": "succeed", +"message": "注册成功" +} +``` +- 用户名已存在: +```json +{ +"type": "register", +"status": "error_0", +"message": "用户名已存在" +} +``` +- 用户名格式错误: +```json +{ +"type": "register", +"status": "error_2", +"message": "用户名长度必须在2到20个字符之间" +} +``` +- 密码格式错误: +```json +{ +"type": "register", +"status": "error_1", +"message": "密码长度必须在4到20个字符之间" +} +``` +- 其他错误 +```json +{ +"type" = "register", +"status" = "error_-1", +"message" = "用户名或密码不能为空" +} +``` + +### 2. 登录 +- **请求**: +```json +{ +"type": "login", +"username": string, +"password": string +} +``` + +- **响应**: +- 成功: +```json +{ +"type" = "login", +"status" = "succeed", +"message" = "登录成功", +"username" = string, +"userid" = string +} +``` +- 账号或密码错误: +```json +{ +"type" = "login", +"status" = "error_0", +"message" = "用户名或密码错误" +} +``` +- 密码格式错误: +```json +{ +"type" = "login", +"status" = "error_1", +"message" = "密码长度不能超过20个字符" +} +``` +- 用户名格式错误: +```json +{ +"type" = "login", +"status" = "error_2", +"message" = "用户名长度必须在2到20个字符之间" +} +``` +- 其他错误: +```json +{ +"type" = "login", +"status" = "error_-1", +"message" = "用户名或密码不能为空" +} +``` + +### 3. 发送聊天消息 +- **请求**: +```json +{ +"type" = "chat"; +"message" = string; +"msgtype" = MessageType; //MessageType定义在下面的条目上 +"userid" = string; +"token" = null; // 可空,因为目前没搞这个。所以可以不用发送token。 +} +``` + +- **响应**: +- 成功: +```json +{ +"type" = "chat"; +"userid" = "Unid"; +"user" = "Unnamed"; +"status" = string; +"message" = string; +"avatar" = null; +"msgtype" = MessageType; //MessageType定义在下面的条目上 +"timestamp" = DateTime(例如:2025-06-15T12:41:26.6322042+08:00); +} +``` + +### MessageType格式 +MessageType在服务器的定义如下,可以先查看关于[C# 枚举(Enum)](https://www.runoob.com/csharp/csharp-enum.html)的文章。 +```C# +public enum MessageType +{ + Text, + Image,//图片 + File,//文件 + System//系统信息 +} +``` +所以在`MessageType.Text`的值为`0`。以此类推,`MessageTypeImage`值为`1`. + +表现在json文本中为 +```json +{ +"msgtype" = 0 +} +``` +键为`"msgtype"`的值为`MessageType.Text`。 \ No newline at end of file