First, the premise preparation
- Create the UniApp project
- Head to the UniApp plugin market to search for the real-time messaging SDK plug-in
- Since the call invitation is usually accompanied by audio and video, it is recommended to use cloud packaging (this project uses cloud packaging).
- The project must be entered after import
manifest.json
Added the cloud plug-in to the App native plug-in - Important: after adding the plug-in, you must make a custom base to run on the real machine using the custom base
See similar example stepsUni audio and video plug-in integration steps
Second, the development of
1. Development ideas
-
Call invitation needs global listening, can be self-encapsulated into JS where needs where to tune. (Do not use the original form for real-time messaging and call invitations because of global use)
-
The call invitation is initialized only once and is not destroyed. You can place the instance initialization in the onLaunch method of app.vue
Development of 2.
-
Create files such as until/rtm.js and until/config.js in the root directory
-
Config file to set basic information
.RTM_APPID: "".// RTM Application ID uid: "123".// Local user id (custom, as required) Copy the code
-
Encapsulation RTM. Js.
-
Introduce and use plug-ins
// Import the config configuration import Config from "./config.js"; Config.RTM_APPID // RTM real-time message plug-in introduced const rtmModule = uni.requireNativePlugin('AR-RtmModule'); Copy the code
-
Encapsulated Call Invitation
// RTM real-time message encapsulation const RTM = { / / initialization init: async() = > {// Initialize the callback await rtmModule.setCallBack(res= > { RTM.callBack(res) }) // Initialize the instance await rtmModule.createInstance({ "appId": Config.RTM_APPID }, res= > { console.log(res); }) // Log in to RTM await rtmModule.login({ "token": ""."userId": Config.uid }, (res) = >{})// Use RTM call invite (set listener for invite call instance) await rtmModule.setCallEventListener() }, / / callback callBack: (res) = > { switch (res.rtmEvent) { // SDK and RTM system connection status change callback. case 'onConnectionStateChanged': console.log(res.state, res.reason) break; // Receive a point-to-point message callback case 'onPeerMessageReceived': console.log(res.text, res.peerId) break; // Return to calling party: the called party has accepted the call invitation case 'onLocalInvitationAccepted': console.log(res) break; // Return to caller: The call invitation has been cancelled case 'onLocalInvitationCanceled': console.log(res) break; // Return to the calling party: the call invitation process failed case 'onLocalInvitationFailure': console.log(res) break; // Return to the calling party: the called party has received the call invitation case 'onLocalInvitationReceivedByPeer': console.log(res) break; // Return to the calling party: the called party has rejected the call invitation case 'onLocalInvitationRefused': console.log(res) break; // Return to the called party: the call invitation is accepted successfully case 'onRemoteInvitationAccepted': console.log(res) break; // Return to the called party: The calling party has canceled the call invitation case 'onRemoteInvitationCanceled': console.log(res) break; // Return to called party: The call invitation process from the calling party failed case 'onRemoteInvitationFailure': console.log(res) break; // Return to called: receive a call invitation case 'onRemoteInvitationReceived': console.log(res) break; // Return to the called party: the call invitation is rejected successfully case 'onRemoteInvitationRefused': console.log(res) break; default: break; } // console.log(res); }, // Check whether all users are online queryPeersOnlineStatus: async function(peerIdLits) { return await new Promise((resolve, reject) = > { rtmModule.queryPeersOnlineStatus({ "peerIds": peerIdLits }, (res) = >{ resolve(res); })})},// Accept the call invitation from the other party acceptRemoteInvitation: function(calleeId, info = "") { console.log('Accept incoming call invitation'); rtmModule.acceptRemoteInvitation({ "calleeId": calleeId, // For the called party to obtain the user ID of the calling party "response": info ? JSON.stringify(info) : "" // Invite response }, (res) = > { console.log(res.code === 0 ? ' ' : "Call to acceptRemoteInvitation failed to accept a call invitation from the other party."); })},// Reject the call invitation from the other party refuseRemoteInvitation: function(userId, info = "") { rtmModule.refuseRemoteInvitation({ "calleeId": userId, "response": info ? JSON.stringify(info) : "" // Invitation content }, (res) = > { console.log(res.code === 0 ? "" : "CancelLocalInvitation failed to cancel the call."); }); }, // Cancel the call invitation cancelLocalInvitation: function(calleeId, info = "") { rtmModule.cancelLocalInvitation({ "calleeId": calleeId, // The user ID of the caller "content": info ? JSON.stringify(info) : "" // Invitation content }, (res) = > { console.log(res.code === 0 ? "" : "CancelLocalInvitation failed to cancel the call."); })}Copy the code
-
-
Global call (mount the wrapped RTM method to the app
import RTM from './rtm.js' Vue.prototype.$RTM = RTM Copy the code
-
RTM initialization
Added to app.vue
onLaunch: function() { console.log('When App Launch completes initialization'); this.$RTM.init(); }, Copy the code
Since RTM needs to be used globally, follow the program. Error: [JS Framework] Failed to receiveTasks, instance (1) is not available. Error: [JS Framework] Failed to receiveTasks, instance (1) is not available Error is normal, because the debugging, RTM initialization operation for many times
-
Call invitation related normal invocation
-
Pay attention to
When the method is called but the plug-in’s method is not called:
- See what the current method uses
RtmModule. Related methods
If there is a - will
rtmModule
Object to see if it is normal
- See what the current method uses