Thinkphp integrates with AJAX long polling to implement in-depth explanation of PC and APP push, thinkphpajax

Source: Internet
Author: User

Thinkphp integrates with AJAX long polling to implement in-depth explanation of PC and APP push, thinkphpajax

Preface

This article mainly introduces Thinkphp's use of AJAX long polling to push PCs and apps. We will share Thinkphp with you for your reference and learning. Let's talk about Thinkphp in detail.

Implementation Logic

After an operation (such as creating a new announcement) is triggered, a message is pushed to all or specified users of the APP or mobile WEB.

Regardless of the performance, it is always used by some people. The implementation is as follows (based on Thinkphp5 message push ):

PHP long polling

/** Long round-robin API query interface */public function id_log () {if (request ()-> isPost ()) {$ id = $ this-> param ['id']; set_time_limit (0); $ id_log = Db: name ('table')-> alias ('C ') -> join ('table CC', 'C. youname = cc. youname', 'left')-> join ('table A', 'CC. youname =. youname', 'left')-> join ('table U', 'C. youname = u. youname', 'left')-> field ('')-> where ('', $ id)-> order ('Log _ time desc ') -> limit (1)-> select (); while (true) {if ($ id_log) {$ id_log_set = Db: name ('table2 ') -> where ('', $ id)-> limit (1)-> setField ('Log _ flag', '1 '); $ this-> response ($ id_log) ;}$ this-> wrong (404100); usleep (2000 );}};}

Replace your table with related fields.

How to create push messages

/*** Create message log trigger Operation Method * @ param string $ log_content log Content * @ param string $ log_type Log Type * @ param int $ log_c_id A user ID * @ param string $ log_user B user ID * @ param string $ log_admin PC * @ param string $ log_status * @ return array */function createLog ($ log_c_id, $ log_type, $ log_content, $ log_admin, $ log_user, $ log_status) {$ data = ['Log _ c_id '=> $ log_c_id, 'Log _ ip' => get_client_ip (), 'Log _ admin' => $ log_admin, 'Log _ time' => date ("Y-m-d H: I: s ", time (), 'Log _ url' => get_url (), 'Log _ type' => $ log_type, 'Log _ content' => $ log_content, 'Log _ user' => $ log_user, 'Log _ status' => $ log_status]; $ logadd = \ think \ Db: name ('table ') -> insertGetId ($ data); return $ logadd ;}

Use the data inventory message and detect and design the flag form to push, otherwise offline messages will not be pushed, the same should be changed to your own data structure!

AJAX long polling

Var getting = {url: '{: url ("youapi/url")}', dataType: 'json', success: function (res) {$ (". count "). text ($ ('. llt '). children (). size (); $ (". llt "). remove (); if (res. length! = Null) {$. each (res, function (index, item) {$ (". notification "). append ('<li class = "llt" data-ccid = "' + item. id + '"> <a href =" {: url ("rel =" external nofollow "admin/carloan/edit ")}? Id = '+ item. log_c_id + '"> <span class =" label-info "> <I class =" icon-bullhorn "> </I> </span> <span class =" message "> '+ item. u_name + ':' + item. log_content + '</span> <span class = "time"> <span class = "liveTime">' + item. log_time + '</span> </a> </li>');}) ;}else {}}; // The Key is here, ajax regularly accesses the server and keeps getting data. Here is a one-second request. Window. setInterval (function () {$. ajax (getting)}, 1000 );

Summary

Well, that's probably the case. The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.