添加 API.md
This commit is contained in:
parent
97e8f4e82a
commit
fd25d57be5
159
API.md
Normal file
159
API.md
Normal file
@ -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`。
|
Loading…
x
Reference in New Issue
Block a user