草庐IT

uni-app技术分享| uni-app转小程序-实时消息

anyRTC 2023-03-28 原文
微信小程序 实现实时消息与 uniapp 转码成微信小程序 实现实时消息两者是一样的,区别仅仅是一个是原生小程序一个是 uniapp 转码成小程序。 本文主要简单实现点对点消息与呼叫邀请等相关功能实现。 uniapp转码成小程序逻辑与小程序逻辑基本一致。

引入 RTM SDK

使用 web RTM-SDK 即可,小程序的实时消息与 WEB 的实时消息共用 SDK。

使用 1.0.5 版本,否则转成小程序时可能会无法正常使用

npm i ar-rtm-sdk@1.0.5 // 引入 anyRTM import ArRTM from "ar-rtm-sdk"

代码封装

你可以在页面里边直接调用,也可单独封装成一个 js。 本人喜欢将 RTM 封装成一个 js 文件。

本地存放

const Store = { // RTM 客户端 rtmClient: null, // 主叫邀请实例 localInvitation: null, // 被叫收到的邀请实例 remoteInvitation: null, }

回调封装

本文仅进行简单封装,如需更复杂逻辑请自行更改。

// RTM 监听事件 const rtmEvent = { // 主叫:被叫已收到呼叫邀请 localInvitationReceivedByPeer: () => { uni.hideToast(); uni.showToast({ title: '被叫已收到呼叫邀请', icon: 'none', duration: 2000, mask: true, }); }, // 主叫:被叫已接受呼叫邀请 localInvitationAccepted: async (response) => { console.log("主叫:被叫已接受呼叫邀请", response); uni.hideToast(); uni.showToast({ title: '被叫接受呼叫邀请', icon: 'none', duration: 2000, mask: true, }); }, // 主叫:被叫拒绝了你的呼叫邀请 localInvitationRefused: (response) => { console.log("主叫:被叫拒绝了你的呼叫邀请", response); uni.hideToast(); uni.showToast({ title: '被叫拒绝呼叫邀请', icon: 'none', duration: 2000, mask: true, }); }, // 主叫:呼叫邀请进程失败 localInvitationFailure: (response) => { console.log("主叫:呼叫邀请进程失败", response); uni.hideToast(); uni.showToast({ title: '呼叫邀请失败', icon: 'error', duration: 2000, mask: true, }); }, // 主叫:呼叫邀请已被成功取消 (主动挂断) localInvitationCanceled: () => { console.log("主叫:呼叫邀请已被成功取消 (主动挂断)"); }, // 被叫:监听收到来自主叫的呼叫邀请 RemoteInvitationReceived: async (remoteInvitation) => { console.log("监听收到来自主叫的呼叫邀请", remoteInvitation); // 监听回调 rtmInternal.inviteProcessing(remoteInvitation) // 显示模态弹窗 uni.showModal({ title: '提示', content: '收到来自主叫的呼叫邀请', cancelText: '拒绝', confirmText: '接听', success: function(res) { if (res.confirm) { console.log('用户点击确定'); remoteInvitation.accept(); } else if (res.cancel) { console.log('用户点击取消'); remoteInvitation.refuse(); } } }); }, // 被叫:监听接受呼叫邀请 RemoteInvitationAccepted: async () => { console.log("被叫 接受呼叫邀请"); uni.hideToast(); uni.showToast({ title: '接受呼叫邀请', icon: 'success', duration: 2000, mask: true, }); }, // 被叫:监听拒绝呼叫邀请 RemoteInvitationRefused: () => { console.log("被叫 拒绝呼叫邀请"); uni.hideToast(); uni.showToast({ title: '拒绝呼叫邀请', icon: 'success', duration: 2000, mask: true, }); }, // 被叫:监听主叫取消呼叫邀请 RemoteInvitationCanceled: () => { console.log("主叫 取消呼叫邀请"); uni.hideToast(); uni.showToast({ title: '主叫取消呼叫', icon: 'success', duration: 2000, mask: true, }); }, // 被叫:监听呼叫邀请进程失败 RemoteInvitationFailure: () => { console.log("被叫 呼叫邀请进程失败"); uni.hideToast(); uni.showToast({ title: '呼叫邀请失败', icon: 'error', duration: 2000, mask: true, }); }, // 收到来自对端的点对点消息 MessageFromPeer: (message, peerId) => { console.log("收到来自对端的点对点消息", message, peerId); uni.showToast({ title: '收到' + peerId + '的点对点消息:' + message.text, icon: 'none', duration: 1000 * 5 }) }, // 通知 SDK 与 RTM 系统的连接状态发生了改变 ConnectionStateChanged: (newState, reason) => { console.log("系统的连接状态发生了改变", newState); switch (newState) { case "CONNECTED": uni.hideLoading(); // SDK 已登录 RTM 系统 uni.showToast({ title: 'RTM 连接成功', icon: 'success', mask: true, }) break; case "ABORTED": uni.showToast({ title: 'RTM 停止登录', icon: 'error', mask: true, }); console.log("RTM 停止登录,重新登录"); break; default: wx.showLoading({ title: 'RTM 连接中', mask: true, }) break; } } }

登录 RTM 系统

所有与 rtm 相关操作都需要登录后才可正常使用 成功登录后即可监听相关回调

// 初始化 export const InItRtm = async (Config) => { // 创建 RTM 客户端 Store.rtmClient = await ArRTM.createInstance(Config.AppID); // RTM 版本 console.log("RTM 版本", ArRTM.VERSION); uni.showLoading({ title: '登录中', mask: true }) // 登录 RTM await Store.rtmClient.login({ token: "", uid: Config.userId }).then(() => { uni.hideLoading(); uni.showToast({ title: '登录成功', icon: 'success', duration: 2000 }) console.log("登录成功"); // 监听收到来自主叫的呼叫邀请 Store.rtmClient.on( "RemoteInvitationReceived", rtmEvent.RemoteInvitationReceived ); // 监听收到来自对端的点对点消息 Store.rtmClient.on("MessageFromPeer", rtmEvent.MessageFromPeer); // 通知 SDK 与 RTM 系统的连接状态发生了改变 Store.rtmClient.on( "ConnectionStateChanged", rtmEvent.ConnectionStateChanged ); }).catch((err) => { Store.userId = ""; uni.hideLoading(); uni.showToast({ icon: 'error', title: 'RTM 登录失败', mask: true, duration: 2000 }); console.log("RTM 登录失败", err); }); }

逻辑方法封装

// RTM 内部逻辑 export const rtmInternal = { ... }

查询呼叫用户是否在线

// 查询呼叫用户是否在线 peerUserQuery: async (uid) => { const oUserStatus = await Store.rtmClient.queryPeersOnlineStatus([uid]); if (!oUserStatus[uid]) { uni.showToast({ title: '用户不在线', icon: 'error', duration: 2000, mask: true, }); return false; } return true; },

发起呼叫

// 主叫发起呼叫 inviteSend: async (peerUserId) => { Store.localInvitation = await Store.rtmClient.createLocalInvitation( peerUserId ) // 设置邀请内容 // Store.localInvitation.content = JSON.stringify({}); // 事件监听 // 监听被叫已收到呼叫邀请 Store.localInvitation.on( "LocalInvitationReceivedByPeer", rtmEvent.localInvitationReceivedByPeer ); // 监听被叫已接受呼叫邀请 Store.localInvitation.on( "LocalInvitationAccepted", rtmEvent.localInvitationAccepted ); // 监听被叫拒绝了你的呼叫邀请 Store.localInvitation.on( "LocalInvitationRefused", rtmEvent.localInvitationRefused ); // 监听呼叫邀请进程失败 Store.localInvitation.on( "LocalInvitationFailure", rtmEvent.localInvitationFailure ); // 监听呼叫邀请已被成功取消 Store.localInvitation.on( "LocalInvitationCanceled", rtmEvent.localInvitationCanceled ); // 发送邀请 Store.localInvitation.send(); },

取消呼叫

发起者主动取消呼叫邀请

callCancel: () => { if (Store.localInvitation) { Store.localInvitation.cancel() } },

被叫邀请回调绑定

// 被叫收到呼叫邀请处理(给收到的邀请实例绑定事件) inviteProcessing: async (remoteInvitation) => { // 监听接受呼叫邀请 remoteInvitation.on( "RemoteInvitationAccepted", rtmEvent.RemoteInvitationAccepted ); // 监听拒绝呼叫邀请 remoteInvitation.on( "RemoteInvitationRefused", rtmEvent.RemoteInvitationRefused ); // 监听主叫取消呼叫邀请 remoteInvitation.on( "RemoteInvitationCanceled", rtmEvent.RemoteInvitationCanceled ); // 监听呼叫邀请进程失败 remoteInvitation.on( "RemoteInvitationFailure", rtmEvent.RemoteInvitationFailure ); },

点对点消息发送

// 发送消息 sendMessage: (uid, message) => { console.log("发送消息", uid, message); Store.rtmClient && Store.rtmClient.sendMessageToPeer({ text: JSON.stringify(message) }, uid).catch(err => { console.log("发送消息失败", err); }); },

简单页面

html

<view class="content"> <view class=""> <text>用户 ID:{{userId}}</text> </view> <view class=""> <!-- 登录 RTM 系统 --> <button v-if="page === 0" type="primary" @click="loginRTM">登录 RTM 系统</button> <!-- --> <view v-else-if="page === 1" class=""> <button type="primary" @click="page=2">呼叫邀请</button> <button type="primary" @click="page=3">发送消息</button> </view> <!-- 呼叫邀请 --> <view v-else-if="page === 2" class=""> <!-- 远端用户 --> <input class="input_automatic" v-model="peerId" type="text" placeholder="请输入远端用户" /> <button type="primary" @click="invitationCall">发起呼叫</button> <button type="primary" @click="invitationCallOff">取消呼叫</button> </view> <!-- 发送消息 --> <view v-else class=""> <input type="text" class="input_automatic" v-model="peerId" placeholder="请输入远端用户" /> <input type="text" class="input_automatic" v-model="sendText" placeholder="请输入消息" /> <button type="primary" @click="sendMessage">发送</button> </view> </view> </view>

js

import { generateNumber } from "../../until/until.js"; // 生成随机数 import { InItRtm, rtmInternal } from "../../until/rtm.js" export default { data() { return { page: 0, // 本地用户 userId: '', // 远端用户 peerId: '', // 发送的信息 sendText: '' } }, created() { // 用户 UID this.userId = generateNumber(4) + '' }, methods: { /** 登录 RTM 系统 */ async loginRTM() { const info = { /** * 必填 anyRTC 为 App 开发者签发的 App ID。每个项目都应该有一个独一无二的 App ID。 * 如果你的开发包里没有 App ID,请从anyRTC官网(https://www.anyrtc.io)申请一个新的 App ID */ AppID: '', userId: this.userId } await InItRtm(info); this.page = 1 }, /** 呼叫邀请 */ async invitationCall() { if (this.peerId === '') return uni.showToast({ title: '请输入远端用户', icon: 'error', }); if (this.peerId === this.userId) return uni.showToast({ title: '禁止远端用户与本地用户一致', icon: 'none', }); // 查询用户是否在线 const state = await rtmInternal.peerUserQuery(this.peerId); if (state) { rtmInternal.inviteSend(this.peerId) } else { return uni.showToast({ title: '用户不在线', icon: 'error', }); } }, invitationCallOff() { rtmInternal.callCancel() }, /** 发送消息 */ async sendMessage() { if (this.peerId === '') return uni.showToast({ title: '请输入远端用户', icon: 'error', }); if (this.peerId === this.userId) return uni.showToast({ title: '禁止远端用户与本地用户一致', icon: 'none', }); if (this.sendText === '') return uni.showToast({ title: '请输入发送信息', icon: 'error', }); // 查询用户是否在线 const state = await rtmInternal.peerUserQuery(this.peerId); if (state) { rtmInternal.sendMessage(this.peerId, this.sendText) } else { return uni.showToast({ title: '用户不在线', icon: 'error', }); } } } }

style

.input_automatic { border: 1px solid; height: 40px; border-radius: 4px; padding: 0 12px; }

有关uni-app技术分享| uni-app转小程序-实时消息的更多相关文章

  1. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  2. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  3. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  4. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  5. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  6. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  7. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  8. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

  9. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  10. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

随机推荐