Ionic/cordova Instant Messaging Solution (UP)

Source: Internet
Author: User

WebApp Instant Messaging Solution at the beginning always look for some web-side third-party, in fact, do mobile or more recommend the use of plug-in to introduce native third-party better. Of course also tried to use the WebSocket protocol to do, tried before but the perfect implementation on the PC, at that time there are some problems on the mobile, but the development of the task of tension later abandoned. This chapter briefly describes the use of cloud-Cordova to achieve instant communication.

Related Documents http://www.rongcloud.cn/docs/cordova.html

1, register a developer account on the cloud website, create an app to get Appkey,appsecret

2, import the plugin in your own project, the command is as follows

Ionic plugin Add Https://github.com/rongcloud/cordova-plugin-rongcloud-im 3, let the server-side configuration Appsecret provide an interface to the front-end, Return token 4 with the UserID and other parameters to connect to the Cloud SDK after the front end gets token:

Initializing the SDK

Rongcloudlibplugin.init ({

             appKey:  "z3v46kbv8v30"

         },
        function (ret,  ERR)  {
          if  (ret)  {
             console.log (' init: '  +  Json.stringify (ret));
          }
          if  (Err)  {
             console.log (' Init error: '  +  Json.stringify (err));
          }
        }  );
   

      
Get account status Rongcloudlibplugin.setconnectionstatuslistener (
function (ret, err) {
if (ret) {
Console.log (' Setconnectionstatuslistener: ' + json.stringify (ret));
if (Ret.result.connectionStatus = = ' kicked ') {
Alert (' Your account has been logged in on another side! ');
$rootScope. Hidetabs = false;
$ionicHistory. ClearCache ();
$state. Go (' login ');
}
}
if (err) {
Console.log (' Setconnectionstatuslistener error: ' + json.stringify (err));
}
});

Connect to Cloud
Rongcloudlibplugin.connect ({

Token acquired by the server interface
Token:token

},
function (ret, err) {
if (ret) {
Console.log (' Connect: ' + json.stringify (ret));
$rootScope. curuid = ret.result.userId;
$rootScope. $apply ();
Myutil.setuserid (RET.RESULT.USERID);
$state. Go (' tab.friends ', {
UserId:ret.result.userId
}, {
Reload:true
});
}
if (err) {
Console.log (' init error: ' + json.stringify (err));
}
});

Listening messages

Rongcloudlibplugin.setonreceivemessagelistener (
function (ret, err) {
if (ret) {
Console.log (' Setonreceivemessagelistener: ' + json.stringify (ret));
var data=json.stringify (ret)
$rootScope. Arrmsgs.push (Json.stringify (ret.result.message));
$rootScope. $apply ();
}
if (err) {
Console.log (' Setonreceivemessagelistener error: ' + json.stringify (err));
}
});

Message list

Rongcloudlibplugin.getconversationlist (
function (ret, err) {
if (ret) {
Console.log (' Setonreceivemessagelistener: ' + json.stringify (ret));
var data=json.stringify (ret)
$rootScope. Arrmsgs.push (Json.stringify (ret.result.message));
$rootScope. $apply ();
}
if (err) {
Console.log (' Setonreceivemessagelistener error: ' + json.stringify (err));
}
});

Chat history data

Rongcloudlibplugin.gethistorymessages ({

Conversation type (single chat)
Conversationtype: ' PRIVATE ',

Conversation Target ID
Targetid: ' 1234 ',

Count:5,
Oldestmessageid:oldestmessageid
},
function (ret, err) {
if (ret) {
Console.log ("gethistorymessages:" + json.stringify (ret));
var result = new Array (), TMP;
for (var i = ret.result.length-1; I >= 0; i--) {
TMP = Ret.result[i];
TMP = MYUTIL.RESOLVEMSG (TMP);
Result.push (TMP);
}
var Hisarr = Result.concat ($scope. hismsgs);
$scope. hismsgs = Hisarr;
}
if (err) {
Alert ("Gethistorymessages error:" + json.stringify (err));
}
});

Get dialog

Rongcloudlibplugin.getconversation ({

Conversation type (single chat)
Conversationtype: ' PRIVATE ',

Conversation Target ID
Targetid: ' 1234 ',
},
function (ret, err) {
if (ret) {
Console.log (' Setonreceivemessagelistener: ' + json.stringify (ret));
var data=json.stringify (ret)
$rootScope. Arrmsgs.push (Json.stringify (ret.result.message));
$rootScope. $apply ();
}
if (err) {
Console.log (' Setonreceivemessagelistener error: ' + json.stringify (err));
}
});

5, chat core functions are listed below is the UI. The next section of the write interface includes the small red dots and the non-reading processing.


Ionic/cordova Instant Messaging Solution (UP)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.