With the development of technology, modern website, more and more application forms;
No longer the need for users to refresh the page as before;
Server-side can proactively push data to the user, more timely;
More prominent is the instant Messenger chat online;
What we're going to build is something like a Web-page version of this feature;
Example Project: Http://git.oschina.net/shuaibai123/thinkphp-bjyadmin
A: Registered cloud account
If we are not using instant Messaging as our main business, then we recommend the use of third-party services; here take the cloud as an example;
Official website: http://www.rongcloud.cn/
Register for Cloud, create apps, get App key and app Secret;
Two: Setting Configuration items
/application/common/conf/config.php
' Rong_is_dev ' and whether true,//is in development
' Rong_dev_app_key ' + ' 8luwapkvu3xwl ',//Cloud development environment KEY for test use only
' Rong_dev_app_secret ' + ' 1aw1d7f6td25 ', SECRET for test use only in the cloud development environment
' Rong_pro_app_key ' + ', '//cloud KEY in the production environment
' Rong_pro_app_secret ' and ', '//SECRET in the cloud production environment
Replace key and Sercet with your own application;
Three: integrated PHP part SDK
Introducing Cloud Sdk:/thinkphp/library/org/xb/rongcloud.class.php
Common functions:/application/common/common/function.php
/**
* Get the corresponding key and secret according to the configuration item
* @return Array Key and secret
*/
function Get_rong_key_secret () {
Determine whether you need a development environment or a production environment key
if (C (' Rong_is_dev ')) {
$key =c (' Rong_dev_app_key ');
$secret =c (' Rong_dev_app_secret ');
}else{
$key =c (' Rong_pro_app_key ');
$secret =c (' Rong_pro_app_secret ');
}
$data =array (
' Key ' = $key,
' Secret ' = $secret
);
return $data;
}
/**
* Get Cloud Tokens
* @param integer $uid User ID
* @return Integer token
*/
function Get_rongcloud_token ($uid) {
Get tokens from the database
$token =d (' Oauthuser ')->gettoken ($uid, 1);
If you have token, return.
if ($token) {
return $token;
}
Get user nickname and Avatar
$user _data=m (' Users ')->field (' Username,avatar ')->getbyid ($UID);
User does not exist
if (Empty ($user _data)) {
return false;
}
Get Avatar URL format
$avatar =get_url ($user _data[' Avatar ');
Get Key and Secret
$key _secret=get_rong_key_secret ();
Instantiate a cloud of melting
$rong _cloud=new \org\xb\rongcloud ($key _secret[' key ', $key _secret[' secret ']);
Get token
$token _json= $rong _cloud->gettoken ($uid, $user _data[' username '), $avatar);
$token _array=json_decode ($token _json,true);
Failed to get token
if ($token _array[' code ']!=200) {
return false;
}
$token = $token _array[' token '];
$data =array (
' UID ' = $uid,
' Type ' =>1,
' Nickname ' = $user _data[' username '],
' Head_img ' = $avatar,
' Access_token ' = $token
);
Insert Database
$result =d (' Oauthuser ')->adddata ($data);
if ($result) {
return $token;
}else{
return false;
}
}
/**
* Updated Melt Cloud Head image
* @param integer $uid User ID
* @return Boolear operation is successful
*/
function Refresh_rongcloud_token ($uid) {
Get user nickname and Avatar
$user _data=m (' Users ')->field (' Username,avatar ')->getbyid ($UID);
User does not exist
if (Empty ($user _data)) {
return false;
}
$avatar =get_url ($user _data[' Avatar ');
Get Key and Secret
$key _secret=get_rong_key_secret ();
Instantiate a cloud of melting
$rong _cloud=new \org\xb\rongcloud ($key _secret[' key ', $key _secret[' secret ']);
Update your cloud Avatar
$result _json= $rong _cloud->userrefresh ($uid, $user _data[' username '), $avatar);
$result _array=json_decode ($result _json,true);
if ($result _array[' code ']==200) {
return true;
}else{
return false;
}
}
Write a controller to get token, buddy list avatar and user name:/application/api/controller/rongcontroller.class.php
/**
* Get token
*/
Public Function Get_token () {
Get User ID
$uid =get_uid ();
Get token
$token =get_rongcloud_token ($UID);
$data =array (
' Token ' = $token
);
Ajax_return ($data, ' gaining success ', 0);
}
/**
* Pass one or more user IDs
* Get user profile picture username; Use to group Friends List
*/
Public Function Get_user_info () {
$uids =i (' post.uids ');
Combining where array conditions
$map =array (
' ID ' =>array (' in ', $uids)
);
$data =m (' Users ')
->field (' Id,username,avatar ')
->where ($map)
->select ();
Ajax_return ($data, ' Get User data successful ', 0);
}
IV: Integrated front-end Section
HTML to introduce the integration of Cloud JS SDK;
<script src= "Http://cdn.ronghub.com/RongIMLib-2.0.6.beta.min.js" ></script>
<script src= "Http://cdn.ronghub.com/RongEmoji-2.0.2.beta.min.js" ></script>
And the following is my painful study of the cloud-fusing SDK after simplifying the use of the method;
/public/statics/rongcloud/js/main.js
Then the HTML can be configured;
/tpl/home/index/user1.html
/tpl/home/index/user2.html
The end result is this:
Of course, the style can be changed by itself;
When testing a project example, remember to use two different browsers to open a link representing two users respectively;
Used to simulate is two users in chat;
This article for Bai Jun Remote original article, reprinted without contact with me, but please specify from Bai Jun remote blog http://baijunyao.com
Thinkphp Integration series of cloud Instant Messaging Live Chat