Home > php教程 > php手册 > body text

PHP implements instant chat (based on Rongyun Communication)

WBOY
Release: 2016-08-25 10:19:44
Original
1821 people have browsed it

Real-time chat in php (based on Rongyun Communication) is currently just a simple example.
Today I saw a very interesting project, an instant messaging tool. Although there are popular chat tools like QQ, it is still interesting to build one yourself.
This case is based on Rongyun Communication. The official website address is http://www.rongcloud.cn/. It has many functions. Since I don’t have a good-looking page, I directly use their plug-in. According to my observation, the plug-in only has the function of recent contacts. . Then let’s implement it according to the simple case first. If you like it, you can continue to study it yourself!
First, you need to register a Rongyun account --- and then create an instance yourself --- check various KEYs. Please follow the instructions on the official website for details. The example I gave is one of my test keys, which can be used directly. It's still a tp5 framework. I don't know how to configure the tp5 framework, so I won't say anything else. There is no use of a database to implement user information. It is a simple implementation of functions. It is mainly to demonstrate the functions for everyone. Please don’t complain.
Configure config.php for various keys<?php<br /> //Configuration file<br /> return [<br /> <br /> 'APP_KEY' => 'e0x9wycfxxx5q',<br> 'APP_SECRET' => 'F7sI8rkLtv'<br> ]; The key they gave is so short. Do you have it? Let’s continue. I downloaded the official SDK and introduced it to the extend file. It can be called directly. Let’s start the main part, the main method of the chat page:
Index.php<?php<br /> namespace appindexcontroller;<br /> <br /> use rongyunServerAPI;<br /> use thinkController;<br /> <br /> class Index extends Controller<br /> {<br /> Public function _initialize()<br /> {<br /> If (Empty (Cookie ('UID'))) {<br />                $this->redirect( url('login/index') );<br>          }<br> }<br> <br> //Chat main method<br> Public function index()<br> {<br> ​​​​$appKey = config('APP_KEY');<br>         $appSecret = config('APP_SECRET');<br> <br>         $rongYun = new ServerAPI( $appKey, $appSecret);<br> <br>          $tx = "http://www.tk.com/static/images/1.jpg";<br> If( 2 == cookie('uid') ){<br>              $tx = "http://www.tk.com/static/images/2.jpg";<br>          }<br>         $token = $rongYun->getToken( cookie('uid'), cookie('uname'), $tx );<br> <br>         $token = json_decode( $token, true )['token'];<br>           $this->assign([<br> ‘token’ => $token<br> ]);<br>           return $this->fetch();<br> }<br> <br> //All user information<br> Public function userInfo()<br> {<br>          $return['userlist'] = [<br>                                                                                                                                                                                                              ['id' => 2, 'name' => '李思', 'portraitUri' => 'http://www.tk.com/static/images/2.jpg']<br> ];<br>           return json( $return );<br> }<br> <br> //Login user information<br> Public function onLine()<br> {<br>         $return['data'] = [<br>                                                                                                                                                  ['id' => '1', 'status' => true],<br> ['id' => '2', 'status' => true]<br> ];<br>           return json( $return );<br> }<br> }I have written all kinds of codes to death, mainly to demonstrate the effect. Okay, for the rest of the code, you can go to my github to download it and go to the official website to compare it. I mainly demonstrate how to run it. Take a look at login.php:<?php<br /> namespace appindexcontroller;<br /> <br /> use thinkController;<br /> <br /> class Login extends Controller<br /> {<br /> Public function index()<br /> {<br />            return $this->fetch();<br> }<br> <br> Public function doLogin()<br> {<br>          $param = input('param.');<br> <br> If ('Zhang San' == $ Param ['uname']) {<br>                cookie('uid', 1);<br> Cookie('uname', 'Zhang San');<br>           }else if('李思'==$param['uname']){<br> <br> Cookie('uid', 2);<br> Cookie('uname', '李思');<br>         }<br> <br>          $this->redirect( url('index/index') );<br> }<br> } Haha, my login is just to distinguish users (this is not the case for real projects!). As you can see, one user is Zhang San and the other user is Li Si. Just enter the password randomly. Let’s take a look at the demonstration effect:
First visit the homepage. If you are not logged in, you will be redirected to log in:
PHP implements instant chat (based on Rongyun Communication)
PHP implements instant chat (based on Rongyun Communication)
If you are using Zhang San to log in, remember: Zhang San’s ID is 1, which is very important! The username is correct and the password is entered randomly. After logging in, the page is as follows:
PHP implements instant chat (based on Rongyun Communication)
Enter the ID of the session, which is the user ID. Let’s talk to Li Si, Li Si is 2, and then click Set Session, the following page will pop up:
PHP implements instant chat (based on Rongyun Communication)
Let’s start chatting:
PHP implements instant chat (based on Rongyun Communication)

Then reopen a browser, it must be another browser! Log in, use Li Si to log in, you will see:
PHP implements instant chat (based on Rongyun Communication)
I saw Zhang San sent you a message, open it and take a look:
PHP implements instant chat (based on Rongyun Communication)
You can also reply:
PHP implements instant chat (based on Rongyun Communication)
Haha, isn’t it interesting? Of course, I have only implemented the simplest functions now, and those who are interested can continue to study!
Project download address: https://github.com/nick-bai/talking. If you don’t see it, the project is being uploaded. You can see it later. If it helps you, give me a star.


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!