XuqmGroup-Web/docs-site/docs/rn/im.md
XuqmGroup 6cd938cfbc feat(android-sdk): 添加完整的IM客户端SDK实现
- 实现了Android SDK的完整IM功能接口,包括消息、群组、好友、会话等核心功能
- 添加了消息收发、历史记录、撤回编辑等完整的消息操作能力
- 实现了群组管理功能,包括创建、成员管理、权限设置等操作
- 添加了好友关系链管理,支持添加、删除、分组等操作
- 实现了会话管理功能,包括置顶、免打扰、已读状态等
- 添加了黑名单、资料管理、搜索等辅助功能
- 补齐了批量操作接口,提升客户端操作效率
- 实现了WebSocket连接管理和事件监听机制
- 添加了离线消息同步和状态管理功能
2026-05-02 22:57:55 +08:00

202 行
3.7 KiB
Markdown

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

# React Native IM 接入
基于 `@xuqm/rn-im` 模块实现即时通讯功能,使用 WatermelonDB 进行本地消息存储。
---
## 初始化与登录
### 初始化
```ts
import { XuqmSDK } from '@xuqm/rn-common'
await XuqmSDK.initialize({
appKey: 'your_app_key',
logLevel: __DEV__ ? 'debug' : 'warn',
})
```
### 登录
```ts
import { XuqmSDK } from '@xuqm/rn-common'
await XuqmSDK.login({
userId: 'user_001',
userSig: 'your_user_sig_jwt',
})
```
> 如果集成了 `rn-im`,`XuqmSDK.login` 会自动触发 `ImSDK` 连接 WebSocket,业务侧无需单独调用 `ImSDK.login`。
---
## 消息收发
### 监听实时消息
```ts
import { ImSDK } from '@xuqm/rn-im'
ImSDK.addEventListener('message', (msg) => {
console.log('收到消息:', msg.msgType, msg.content)
})
ImSDK.addEventListener('read', (msg) => {
console.log('已读回执:', msg.id)
})
ImSDK.addEventListener('revoke', (data) => {
console.log('消息被撤回:', data.msgId)
})
```
### 发送文本消息
```ts
const msg = await ImSDK.sendMessage(
'user_002', // toId
'SINGLE', // chatType: 'SINGLE' | 'GROUP'
'TEXT', // msgType
'Hello!' // content
)
```
### 发送图片
```ts
const msg = await ImSDK.sendImageMessage(
'user_002',
'SINGLE',
'/path/to/image.jpg', // 本地 URI
800, // 宽
600 // 高
)
```
### 获取历史消息
```ts
// 单聊历史
const history = await ImSDK.fetchHistory('user_002', page, size)
// 群历史
const groupHistory = await ImSDK.fetchGroupHistory('group_xxx', page, size)
```
---
## WatermelonDB 本地存储
`rn-im` 使用 WatermelonDBSQLite存储本地消息,按 `appKey + userId` 自动隔离。
```ts
import { ImDatabase } from '@xuqm/rn-im'
// 消息搜索(本地)
const params: MessageSearchParams = {
keyword: '会议',
toId: 'user_002',
chatType: 'SINGLE',
msgTypes: ['TEXT'],
limit: 20,
}
const results = await ImSDK.searchMessages(params)
```
---
## 群聊
```ts
// 创建群
const group = await ImSDK.createGroup('项目讨论', ['user_002', 'user_003'])
// 群列表
const groups = await ImSDK.listGroups()
// 添加成员
await ImSDK.addGroupMember('group_xxx', 'user_004')
// 移除成员
await ImSDK.removeGroupMember('group_xxx', 'user_004')
// 退出群聊
await ImSDK.leaveGroup('group_xxx')
```
详见 [RN 群聊文档 →](./group)
---
## 会话列表
```ts
// 订阅会话变化
const unsub = ImSDK.subscribeConversations((conversations) => {
console.log(conversations)
})
// 置顶
await ImSDK.setConversationPinned('user_002', 'SINGLE', true)
// 免打扰
await ImSDK.setConversationMuted('group_xxx', 'GROUP', true)
// 标记已读
await ImSDK.markRead('user_002')
```
---
## 离线消息同步
```ts
// 查询离线消息数量(示例)
const count = await ImSDK.offlineMessageCount()
// 同步离线消息(示例)
const messages = await ImSDK.syncOfflineMessages()
```
---
## 断开连接
```ts
ImSDK.disconnect()
```
---
## 类型参考
```ts
interface ImMessage {
id: string
fromId: string
toId: string
chatType: 'SINGLE' | 'GROUP'
msgType: 'TEXT' | 'IMAGE' | 'AUDIO' | 'VIDEO' | 'FILE' |
'LOCATION' | 'NOTIFY' | 'CUSTOM' | 'RICH_TEXT' |
'CALL_AUDIO' | 'CALL_VIDEO' | 'FORWARD' | 'REVOKED'
content: string
status: 'SENDING' | 'SENT' | 'DELIVERED' | 'READ' | 'FAILED' | 'REVOKED'
createdAt: number
}
interface ConversationData {
targetId: string
chatType: 'SINGLE' | 'GROUP'
lastMsgContent: string
lastMsgType: string
lastMsgTime: number
unreadCount: number
isMuted: boolean
isPinned: boolean
}
```
[→ 完整 API Reference](./api)