wiki-files/wg-basic/websocket.md

136 lines
4.6 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.

---
title: 7.即时消息
description: 即时消息模块的使用
published: true
date: 2021-10-09T09:45:06.612Z
tags: wg-basic
editor: markdown
dateCreated: 2021-09-14T06:25:26.887Z
---
# 依赖模块
```xml
<dependency>
<groupId>ink.wgink</groupId>
<artifactId>module-instant-message</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
```
# 配置
```yml
websocket:
url: 127.0.0.1
port: 8081
# 上下文
context: websocket
```
# 如何对接
1. 不论后台还是APP首先需要完成登录
2. 请求接口完成socket登录
- 后台请求接口:`api/websocket/client/login/{clientName}`
- APP请求接口`app/websocket/client/login/{clientName}`
- 请求方法GET
- clientName客户端名称唯一标识非中文
3. 请求接口后拿到会话IDsessionId
4. 初始化 WebSocket
5. 连接打开后,发起 **会话注册**,携带请求后的 **sessionId** 完成会话注册。
6. 注册成功后,可发起 scoket 请求
# 类型与参数
## 1. 消息格式说明
消息格式为 **JSONObject** 字符串
具体内容如下表
| 属性 | 说明 | 类型 | 是否可空 |
| :-: | :-: | :-: | :-: |
| id | 消息ID唯一 | String | 否 |
| type | 消息类型编码 | Integer | 否 |
| isSystem | 是否是系统消息 | Boolean | 否 |
| from | 发送人的 **userId**,如果是系统消息则为:**SYSTEM** | String | 否 |
| to | 接收人的 **userId****userId 列表**。userId 列表为 **英文逗号** 分割的字符串 | String | 否 |
| body | 消息主体,主体的具体格式会根据 **消息编码type** 的变化而变化 | String | 否 |
| timestamp | 消息时间戳 millisecond | Long | 是 |
from与to可以相同
## 2. 消息编码
| 编码 | 说明 | 主体格式 |
| :-: | :-: | :-: |
| 1000 | 注册消息 | RegisterBody 的 JSONObject 字符串 |
| 2001 | 文本消息 | 发送的字符串内容 |
| 3001 | 通知 | NoticeBody 的 JSONObject 字符串 |
| 3003 | 带目标的通知客户端可以根据业务需求动态变更目标target内容 | NoticeBody 的 JSONObject 字符串 |
| 9001 | 消息发送状态 | StatusBody 的 JSONObject 字符串 |
| 9101 | 消息接受状态 | StatusBody 的 JSONObject 字符串 |
## 3. 主体
**RegisterBody**
| 属性 | 说明 | 类型 | 是否可控 |
| :-: | :-: | :-: | :-: |
| sessionId | 登录获取到的会话ID | String | 否 |
**StatusBody**
| 属性 | 说明 | 类型 | 是否可空 |
| :-: | :-: | :-: | :-: |
| code | 状态码 | Integer | 否 |
| status | 状态 | StatusEnum | 否 |
| msg | 说明 | String | 否 |
**NoticeBody**
| 属性 | 说明 | 类型 | 是否可空 |
| :-: | :-: | :-: | :-: |
| title | 标题 | String | 否 |
| msg | 内容 | String | 否 |
| target | 目标,需要客户端点击触发效果时使用,可以为约定好的 url、page 等等 | String | 是 |
| serviceId | 业务ID | String | 是 |
## 4. 状态码
| 编码 | 名称 | 说明 |
| :-: | :-: | :-: |
| 200 | SUCCESS | 成功 |
| 400 | FAILED | 失败 |
| 401 | MESSAGE_ERROR | 消息错误 |
| 402 | SESSION_ERROR | 会话错误 |
| 403 | TYPE_ERROR | 类型编码错误 |
| 404 | BODY_ERROR | 消息体错误 |
| 405 | FROM_ERROR | 来源错误 |
| 406 | TO_ERROR | 接收人错误 |
| 407 | RECEIVE_ERROR | 接收错误 |
# 自定义处理消息业务
1. 实现 `ink.wgink.module.instantmessage.service.IWebSocketTextCustomService` 接口
2.**handle** 方法中完成业务逻辑handle方法接收两个参数第一个参数时当前会话通道第二个参数时消息体
3. 发送消息调用WebSocketChannelManager.getInstance().sendText(channel, message) 方法发送消息
# 通道管理器WebSocketChannelManager
该类不能 **new**,通过 **getInstance()** 方法得到单例对象,提供了如下功能:
| 方法 | 参数 | 说明 |
| :-: | :-: | :-: |
| listOnlineUser | userId: 用户ID | 获取在线用户客户端列表 |
| getOnlineUser | userId: 用户ID, clientName客户端名称 | 获取在线用户 |
| listOnlineUser | userIds: 用户ID列表 | 获取在线用户客户端列表 |
| listOnlineUser | userIds: 用户ID列表, clientName: 客户端名称 | 获取在线用户客户端列表 |
| getOnlineUserBySessionId | sessionId: 会话ID | 通过会话获取用户 |
| getOnlineUserByChannelId | channelId: 通道ID | 通过通道获取用户 |
| sendText | channel: 接收人通道, webSocketClientMessage: 发送内容 | 单发文本消息 |
| sendGroupText | channels: 接收人通道列表, webSocketClientMessage: 发送内容 | 群发文本消息 |
> webSocketClientMessage 与[消息格式说明](#1.消息格式说明) 相同