fonchain-fiee/pkg/common/ws/readme.md
2025-06-12 17:07:49 +08:00

53 lines
1.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# wsscoket 对接说明
## 客户端对接测试页面
[{{服务端地址}}/ws/client](http://127.0.0.1:8088/ws/client)
## 客户端对接websocket流程
### websocket的连接
1. 客户端登录后获取uuid
2. 连接服务端websocket后在10s内发送一下格式的数据否则websocket连接将断开。
uuid请添加登录后获取的如果uuid不正确连接也会断开
```json
{
"type": "register",
"from": "",
"to": "0",
"content": {
"uuid":"用户的uuid"
}
}
```
注册成功后服务端将返回客户端临时id
```json
{"clientId":"02de5759-3f0a-47fa-a79f-afe61c39c5aa"}
```
### weboscket 数据发送测试
消息类型`type="test"`时,客户端将会把`content`内容原路返回,以此来测试最基本的通讯功能。
```json
{
"type": "test",
"from": "用户clientId",
"to": "0",
"content": {
"demo":"testdemo"
}
}
```
### websocket消息类型说明
#### 错误消息
在websocket通讯过程中服务端会对客户端发送过来的消息进行验证。
| type字段 | content字段 | 说明 |
|--------|----------------------|------------------------------|
| Error | Permission denied | 拒绝访问。 此报错一般出现在首次连接验证uuid的时候 |
| Error | Invalid data format | 无效的数据格式。消息内容未按照指定格式书写 |
| Error | Unknown message type | 未知的消息类型。接收到了未定义的type |
**错误消息示例:**
```json
{"type":"Error","content":"Permission denied","from":"0","to":"tempId"}
//{"type":"Error","content":"Invalid data format","from":"0","to":""}
//{"type":"Error","content":"Unknown notice type","from":"0","to":"0"}
```