wkim/README.md
2025-01-11 14:39:57 +08:00

127 lines
3.2 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.

## 悟空IM Flutter Web SDK
![](https://img.shields.io/static/v1?label=platform&message=flutter&color=green) ![](https://img.shields.io/hexpm/l/plug.svg)
[悟空IM]
## 快速入门
#### 引入
```dart
import 'package:wukongimfluttersdk/wkim.dart';
```
**安装方式**
需要手动下载该项目到本地,注意:与你的开发项目在同一目录下
1. 打开pubspec
项目目录下找到 pubspec.yaml
2. 在 dependencies 中添加
```yaml
wukongimfluttersdk:
path: ../wkim/
```
**初始化sdk**
```dart
WKIM.shared.setup(Options.newDefault('uid', 'token'));
```
**初始化IP**
```dart
WKIM.shared.options.getAddr = (Function(String address) complete) async {
// 可通过接口获取后返回
String ip = await HttpUtils.getIP();
complete(ip);
};
```
**连接**
```dart
WKIM.shared.connectionManager.connect();
```
**断开**
```dart
// isLogout true退出并不再重连 false退出保持重连
WKIM.shared.connectionManager.disconnect(isLogout)
```
**发消息**
```dart
WKIM.shared.messageManager.sendMessage(WKTextContent('我是文本消息'), WKChannel(channelID, channelType));
```
## 监听
**连接监听**
```dart
WKIM.shared.connectionManager.addOnConnectionStatus('home',
(status, reason,connectInfo) {
if (status == WKConnectStatus.connecting) {
// 连接中
} else if (status == WKConnectStatus.success) {
var nodeId = connectInfo?.nodeId; // 节点id
// 成功
} else if (status == WKConnectStatus.noNetwork) {
// 网络异常
} else if (status == WKConnectStatus.syncMsg) {
//同步消息中
} else if (status == WKConnectStatus.syncCompleted) {
//同步完成
}
});
```
**消息入库**
```dart
WKIM.shared.messageManager.addOnMsgInsertedListener((wkMsg) {
// todo 展示在UI上
});
```
**收到新消息**
```dart
WKIM.shared.messageManager.addOnNewMsgListener('chat', (msgs) {
// todo 展示在UI上
});
```
**刷新某条消息**
```dart
WKIM.shared.messageManager.addOnRefreshMsgListener('chat', (wkMsg) {
// todo 刷新消息
});
```
**命令消息(cmd)监听**
```dart
WKIM.shared.cmdManager.addOnCmdListener('chat', (cmdMsg) {
// todo 按需处理cmd消息
});
```
- 包含`key`的事件监听均有移除监听的方法,为了避免重复收到事件回掉,在退出或销毁页面时通过传入的`key`移除事件
**发送消息时间**
[使用](https://blog.csdn.net/weixin_37813152/article/details/128712718)
[网络协议] NTP(Network Time Protocol)协议
使用阿里时间
ntp.aliyun.com
ntp1.aliyun.com
ntp2.aliyun.com
ntp3.aliyun.com
ntp4.aliyun.com
ntp5.aliyun.com
ntp6.aliyun.com
ntp7.aliyun.com
**Socket链接逻辑**
将socket链接分成两部分: (链接成功之前,链接成功之后)这里是根据是否有返回消息/结合onDone来判断
1.链接成功之前:(链接中)
链接不成功 时 每隔5秒重新链接。即没有返回消息并且onDone触发时会触发 5秒 后断线重连
当 WKIM.shared.connectionManager.disconnect 时会清理掉计时器。
2.链接成功之后
接首到消息返回,开启心跳检测/网络异常监听
### 许可证
悟空IM 使用 Apache 2.0 许可证。有关详情,请参阅 LICENSE 文件。