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)