uni-app实现实时消息SDK插件

徘徊边缘 提交于 2021-01-10 11:38:28

前言

之前的两篇文章我们介绍了用uni-app实现了实时音视频通话这个功能。没有印象的小伙伴们可以看这两个链接。

Android uni-app 封装原生插件

Android uni-app实现音视频通话

更多的消息资讯可以关注 anyRTC开发者官网

1、效果图

先给大家看一下效果,然后我们再继续讲解。

GitHub下载地址

uni-app-rtm demo:点击下载

扫码下载

2、功能介绍

uni-app跨平台实时消息SDK插件,包含点对点消息、频道消息、呼叫邀请等功能。在同一频道下,用户可以接收和发送消息。从下方的状态栏里面可以到频道内的状态变化,包括人员的更新和消息的更新。

支持平台

  • Android
  • ios

应用场景

1、在线教育

在线教育场景中,适用于大班课和小班课,同学可以实时发送消息与老师进行沟通。

2、娱乐直播

在直播环境中,观众可以实时发送弹幕与主播进行互动。

3、对讲

在铁路或者运输的场景中,同一公司,同一部门之间各个员工之间可以在相同的频道下进行沟通,大大提高工作效率,减少沟通成本。

4、语音群聊

在语音聊天的环境下同时可以支持消息沟通。

3、项目集成

快速集成

  • 下载示例工程,前往anyRTC官网注册账号
  • 创建应用,获取应用的App ID。

appid

  • 下载插件示例工程,制作自定义基座,主要步骤如图,证书问题,请参照申请证书说明。

自定义基座

  • 运行自定义基座

运行自定义基座

4、方法回调

1、login(登录rtm)

{“code”: 0}

2、logout(登出rtm)

{“code”: 0}

3、queryPeersOnlineStatus(查询指定⽤户的在线状态)

{“peerOnlineStatus”:[{“peerId”:“123”,“state”:1}],“code”:0}

4、subscribePeersOnlineStatus(订阅指定单个或多个⽤户的在线状态)

{“code”: 0}

5、unsubscribePeersOnlineStatus(订阅指定单个或多个用户的在线状态)

{“code”: 0}

6、queryPeersBySubscriptionOption(获取某特定内容被订阅的membersOnSuccess用户列表)

{“peers”:[“123”,“456”],@“code”:0}

peers为订阅用户id数组

7、setLocalUserAttributes(全量设置本地用户的属性)

{“code”: 0}

8、addOrUpdateLocalUserAttributes(添加或更新本地用户的属性)

{“code”: 0}

9、deleteLocalUserAttributesByKeys(删除本地用户的指定属性)

{“code”: 0}

10、clearLocalUserAttributes(清空本地用户的所有属性)

{“code”: 0}

11、getUserAttributes(获取指定用户的全部属性)

{“userAttributesList”:{“age”:23,“name”:“Jerry”},“userId”:“123”, “code”:0}

12、getUserAttributesByKeys(获取指定用户指定属性名的属性)

{“userAttributesList”:{“age”:23,“name”:“Jerry”},“userId”:“123”, “code”:0}

13、setChannelAttributes(全量设置某指定频道的属性)

{“code”: 0}

14、addOrUpdateChannelAttributes(添加或更新某指定频道的属性)

{“code”: 0}

15、deleteChannelAttributesByKeys(删除某指定频道的指定属性)

{“code”: 0}

16、clearChannelAttributes(清空某指定频道的属性)

{“code”: 0}

17、getChannelAttributes(查询某指定频道的全部属性)

{“channelAttributes”:{“age”:23,“name”:“Jerry”}, “code”:0}

18、getChannelAttributesByKeys(查询某指定频道指定属性名的属性)

{“channelAttributes”:{“age”:23,“name”:“Jerry”}, “code”:0}

listDic 例如:{“age":23,“name”:“Jerry”}

19、getChannelMemberCount(查询单个或多个频道的成员人数)

{“memberCounts”:[{“channelId”:“909090”,"count”:2}],“code”:0}

20、join(加入频道)

{“code”: 0}

21、leave(离开频道)

{“code”: 0}

22、sendChannelMessage(发送频道消息)

{“code”: 0}

23、getMembers(获取频道成员列表)

{“members”:[{“channelId”:“909090”,“userId”:“123456”}],“code”:0}

24、sendLocalInvitation、acceptRemoteInvitation、refuseRemoteInvitation、cancelLocalInvitation

5、事件回调

1、RtmClientListener

1、onConnectionStateChanged( SDK 与 anyRTC rtm系统的连接状态发生改变回调 )

{
   
   "rtmEvent":"onConnectionStateChanged","state":0,"reason":0}

2、onPeerMessageReceived( 收到点对点消息回调 )

{
   
   "rtmEvent":"onPeerMessageReceived","serverReceivedTs":0,"text":"message","OfflineMessage":0,"messageType":0,"peerId":"123"}

3、onPeersOnlineStatusChanged( 被订阅用户在线状态改变回调 )

{
   
   "rtmEvent":"onPeersOnlineStatusChanged","peersStatus":[{
   
   "peerId":"123","state":0}]}

2、RtmChannelListener

1.onAttributesUpdated (频道属性更新回调)

{
   
   "rtmEvent":"onAttributesUpdated", "attributeList":[{
   
   "key":"name","value":"Jerry","lastUpdateUid":"123","lastUpdateTs":0}]}

2.onMemberCountUpdated (频道成员人数更新回调)

{
   
   "rtmEvent":"onMemberCountUpdated","memberCount": 0}

3.onMemberJoined (远端用户加入频道回调)

{
   
   "rtmEvent":"onMemberJoined","userId": "123","channelId": "909090"}

4.onMemberLeft (频道成员离开频道回调)

{
   
   "rtmEvent":"onMemberLeft","userId": "123","channelId": "909090"}

5.onChannelMessageReceived (收到频道消息回调)

{
   
   "rtmEvent":"onChannelMessageReceived","type":0,"text":"message","serverReceivedTs":0,"isOfflineMessage":true, "userId": "123","channelId": "909090"}

3、RtmCallEventListener

1.onLocalInvitationAccepted (返回给主叫:被叫已接受呼叫邀请)

{
   
   "rtmEvent":"onLocalInvitationAccepted","calleeId": "123","content": "","channelId": "909090","state": 0,"getResponse": "","response": ""}

2.onLocalInvitationCanceled (返回给主叫:呼叫邀请已被取消)

{
   
   "rtmEvent":"onLocalInvitationCanceled","calleeId":"123","content": "","channelId":"909090","state": 0}

3.onLocalInvitationFailure (返回给主叫:呼叫邀请进程失败)

{
   
   "rtmEvent":"onLocalInvitationFailure","calleeId": "123","content": "","channelId":"909090","state": 0,"code": 0}

4.onLocalInvitationReceivedByPeer (返回给主叫:被叫已收到呼叫邀请)

{
   
   "rtmEvent":"onLocalInvitationReceivedByPeer", "calleeId": "123","content": "","channelId": "909090","state":0}

5.onLocalInvitationRefused (返回给主叫:被叫已拒绝呼叫邀请)

{
   
   "rtmEvent":"onLocalInvitationRefused","calleeId":"123", "content":"","channelId":"909090","state": 0,"getResponse": "","response": ""}

6.onRemoteInvitationAccepted (返回给被叫:接受呼叫邀请成功)

{
   
   "rtmEvent":"onRemoteInvitationAccepted","callerId":"123","content":"","channelId":"909090","state": 0,"response": ""}

7.onRemoteInvitationCanceled (返回给被叫:主叫已取消呼叫邀请)

{
   
   "rtmEvent":"onRemoteInvitationCanceled","callerId": "123", "content": "","channelId":"909090","state": 0}

8.onRemoteInvitationFailure (返回给被叫:来自主叫的呼叫邀请进程失败)

{
   
   "rtmEvent":"onRemoteInvitationFailure","callerId":"123","content":"","channelId":"909090","state":0}

9.onRemoteInvitationReceived (返回给被叫:收到一个呼叫邀请)

{
   
   "rtmEvent":"onRemoteInvitationReceived","callerId":"123","content":"","channelId":"909090","state": 0}

10.onRemoteInvitationRefused(返回给被叫:拒绝呼叫邀请成功)

{
   
   "rtmEvent":"onRemoteInvitationRefused","callerId":"123", "content": "","channelId":"909090","state":0,"response": ""}

6、总结

以上就是大部分重要的接口,基本上都已经介绍完了,欢迎大家下载使用。目前anyRTC已经用uni-app实现了实时视频通话和实时消息送达,如果大家还有什么疑问,可以在评论区留言!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!