@dingrtc/rtm
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@dingrtc/rtm

安装

npm install @dingrtc/rtm

使用

@dingrtc/rtm 支持独立使用和配合 dingrtc sdk 使用两种方式:

  • 独立使用:使用rtm实例加入频道,适合只使用rtm的情况
  • 配合 dingrtc使用:与 dingrtc 实例共享入会链接,适合 dingrt和rtm一起使用的情况

独立使用

import RTM from '@dingrtc/rtm';

const rtm = new RTM();

// 参考 https://help.aliyun.com/document_detail/2640080.html 开通 DingRTC 服务
// 获取用户配置以加入频道
await rtm.join({
  appId: '',
  userName: '',
  channel: '',
  uid: '',
  token: ''
});

配合 dingrtc 使用

import DingRTC from 'dingrtc';
import RTM from '@dingrtc/rtm';

const client = DingRTC.createClient();

const rtm = new RTM();
// rtm 和 rtc 共享同一个入会连接
client.register(rtm);

await client.join({
  appId: '',
  userName: '',
  channel: '',
  uid: '',
  token: '',
});

加入会话发布消息

// 加入rtm session
await rtm.joinSession('xxxxxx');
// 发布消息
const message = 'hello world';
const encoder = new TextEncoder();
// 广播
rtm.publish('xxxxxx', encoder.encode(message));

// 点读点发布, 需要双方在同一个RTM会话内
const otherUserId = 'user1';
rtm.publish('xxxxxx', encoder.encode(message), otherUserId);

监听事件

const decoder = new TextDecoder();
// 监听远端消息发布事件
rtm.on('message', (data) => {
  const {
    message, // 消息载体
    uid, // 发布人的uid
    sessionId, // 消息归属的sessionId
    broadcast, // 消息是广播还是点对点发布
  } = data;
  console.log(decoder.decode(message))
})
// 监听rtm会话添加/移除事件
rtm.on('session-add', (session) => {
  console.log(session)
})
rtm.on('session-remove', (session) => {
  console.log(session)
})
// 监听rtm会话成员进出事件
rtm.on('session-user-join', (sessionId, uid) => {
  console.log(sessionId, uid)
})
rtm.on('session-user-left', (sessionId, uid) => {
  console.log(sessionId, uid)
})

Readme

Keywords

Package Sidebar

Install

npm i @dingrtc/rtm

Weekly Downloads

33

Version

1.0.1

License

MIT

Unpacked Size

399 kB

Total Files

6

Last publish

Collaborators

  • shawn_huang
  • villeewang
  • leopano