Thinkphp Integration series of cloud Instant Messaging Live Chat

Source: Internet
Author: User
Tags live chat

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;

650) this.width=650; "title=" Bai Jun Haruka Blog "src=" Http://baijunyao.com/Upload/image/ueditor/20160614/1465916234843508.jpg " alt= "Bai Jun Remote Blog" style= "border:0px;"/>

Two: Setting Configuration items

/application/common/conf/config.php

' Rong_is_dev ' + true,//is in development ' rong_dev_app_key ' = ' 8luwapkvu3xwl ', the//cloud development environment is a KEY for test use only ' Rong_dev_app_secret ' + ' 1aw1d7f6td25 ',//Cloud development environment SECRET only for testing using ' rong_pro_app_key ' + ',//Cloud production environment Key ' Rong_pro_app_secret ' + ',//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

/** *  gets the corresponding key and secret *  @return  array key and secret */function get_ based on the configuration item Rong_key_secret () {    //  Determine whether a development environment or a production environment is required 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 token *  @param   integer  $uid   user id *  @return   Integer   &nbsP;  token */function get_rongcloud_token ($uid) {    //  Get token     $token =d (' Oauthuser ') from the database->gettoken ($uid, 1);     //   return     if  ($token) If token is available  {         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 ();    //  instantiation of Cloud      $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);    //  Get token failed     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;    }}/** *  Update Cloud head  *  @param   integer  $uid   User id *  @return  boolear       operation 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 ();     //  materialized Cloud      $rong _cloud=new \org\xb\rongcloud ($key _secret[' key ', $key _secret[' secret ']);     //  Update Cloud user 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 Username:/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, ' get 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:

650) this.width=650; "title=" Bai Jun Haruka Blog "src=" Http://baijunyao.com/Upload/image/ueditor/20160614/1465918228611040.jpg " alt= "Bai Jun Remote Blog" style= "border:0px;"/>

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

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.