This article mainly introduces about thinkphp combined with Ajax long polling to achieve PC and app push, has a certain reference value, now share to everyone, have the need for friends can refer to
Objective
This article mainly introduces to you about thinkphp combined with Ajax long polling to achieve PC and app push related content, share out for everyone to reference study, talk not to say, come together to see the detailed introduction.
Implementing logic
An action, such as creating a new announcement, triggers all users or designated users who push the message to the app or mobile Web at the same time.
No matter the performance, there will always be someone to use it, implemented as follows (based on THINKPHP5 message push):
PHP Long Polling
/ * * Long Polling 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 =a.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) C19/>->setfield (' Log_flag ', ' 1 '); $this->response ($id _log); } $this->wrong (404100); Usleep (+); } }; }
Change your own table and associated fields.
Push Message Creation Method
/** * Create message Log trigger action method * @param string $log _content Log Contents * @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 ($lo g_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 flag form push, otherwise offline messages can not be pushed, the same to modify 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 label-info" ><i class= "Icon-bullhorn" ></i></span><span class= "message" > ' + item.u_name + ': ' + item.log_content + ' </span><span class= ' time ' ><span class= ' livetime ' > ' + ite M.log_time + ' </span></span></a></li> '); }); }else{}}}; The key here, Ajax timed access to the server, and constantly get data, here is a 1-second request once. Window.setinterval (function () {$.ajax (getting)}, +);