微信小程序快速集成
先按概述安装本地构建的包,并在业务服务端为登录用户签发登录凭证。
创建客户端
在 app.js 中创建单个客户端,用同一个引用建立页面 Behavior:
const {
createDRMiniProgramClient, createDRClientRef, createDRBehavior, locale,
} = require('@deeprespond/im-miniprogram');
const imRef = createDRClientRef();
App({
behavior: createDRBehavior(imRef),
onLaunch() {
this.im = createDRMiniProgramClient({
appKey: '组织#应用', apiUrl: 'https://im.example.com', autoMarkRead: true,
});
imRef.set(this.im);
},
report(error) {
wx.showToast({ title: locale.describeError(error), icon: 'none' });
},
});登录页收到业务凭证后调用:
await getApp().im.auth.loginWithTicket({ ticket });
await getApp().im.connection.waitUntilReady({ timeoutMs: 20000 });客户端会恢复上次会话,登录凭证和 App Secret 不应写死在小程序中。同一 appKey 重复创建返回 client_exists,销毁使用 await im.destroy()。
会话和聊天页
const app = getApp();
Page({
behaviors: [app.behavior],
data: {},
onLoad() { this.drList('list', im => im.conversations.list()); },
onShow() { this.drPageShow(); },
onHide() { this.drPageHide(); },
onUnload() { this.drPageUnload(); },
});<view wx:for="{{list.items}}" wx:key="conversation_key">
{{item.peer || item.group_id}}:{{item.unread_count}}
</view>聊天页使用 this.drMessageList('chat', conversationKey),消息循环的 wx:key 为 _key。发送文字:
await app.im.messages.send({ conversation_key: conversationKey }, {
type: 'text', body: { text: '你好' },
});Component 可由 Behavior 自动接收生命周期;Page 显式调用 drPageShow/Hide/Unload。登录会话改变后绑定自动释放并重建。每个组件的 setData 按回调串行处理,一次 JSON 不超过 512 KiB,消息默认显示最近 100 条。
独立 /bind 入口提供 createDRClientRef、createDRBehavior、bindSnapshot、bindState、DR_STATES;SDK 从分包加载后调用 imRef.set(im)。聊天室通过 drChatroom 绑定,相同房间多个绑定共享进入计数,最后一个解绑才离开。
路径文件和录音
wx.chooseMedia({
count: 1, mediaType: ['image'],
success: result => app.im.messages.send({ to_user: 'alice' }, {
type: 'image', file: { path: result.tempFiles[0].tempFilePath },
}).catch(error => app.report(error)),
});
const recorder = await app.im.files.recordVoice({ maxSeconds: 60 });
await recorder.start();
const voice = await recorder.stop();
await app.im.messages.send({ to_user: 'alice' }, {
type: 'voice', file: voice.file, body: { duration_seconds: voice.duration_seconds },
});
const task = app.im.files.download({ message });
const { path, fileType } = await task;
wx.openDocument({ filePath: path, fileType });视频可传 { path, width, height, duration, thumbnail: { path } }。JPEG/PNG 会去除元数据,MP4 分段处理元数据;无法处理的格式返回 metadata_unremovable,业务明确接受保留元数据时才设置 keepMetadata: true。
app.im.files.localPath(url) 可用于图片缓存,下载任务提供 cancel()。预览图片、打开文档期间可使用 keepForeground() 返回的释放函数,在用户返回后释放,最长保活 60 秒。
Taro React
先导入补齐入口,再使用共享工厂和小程序专用 React 绑定:
import '@deeprespond/im-web/polyfills';
import { createDRMiniProgramClient } from '@deeprespond/im-web/miniprogram';
import { DRProvider } from '@deeprespond/im-web-react/miniprogram';
const im = createDRMiniProgramClient({ appKey: '组织#应用', apiUrl: 'https://im.example.com' });
export default function App({ children }) {
return <DRProvider client={im}>{children}</DRProvider>;
}各包保持同版本。Taro 不需要安装原生预打包包,参考仓库 sdk/miniprogram/example-taro。
独立频道能力
当前仅提供频道、本人会话与成员查询及强类型导出,没有 LiveKit 频道媒体适配。channels.supported 为 false,加入在预留前拒绝,见频道查询与限制。
