Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize AJAX long polling by combining jquery with php

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the combination of jquery and php how to achieve AJAX long polling, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

HTTP is a stateless, one-way protocol. Users can only send a request to the server through the customer server and a response can be sent back by the server. If you want to achieve chat rooms, webQQ, online customer service, mailboxes and other instant messaging applications, it is necessary to use "server push technology (Comet)".

In the traditional AJAX polling method, the customer server queries the server for the latest data at user-defined intervals. This way of pulling data requires a short time interval to ensure the accuracy of the data, but if the time interval is too short, the customer server will send multiple requests to the server in a short time.

Reversing AJAX is called long polling or COMET. The server and the customer server need to maintain a request for a long time, which enables the server to return a message to the client when it has data.

XHTML

JQuery

Here, you use AJAX to request the data.php page to get the value of 'success', and the request time is 80 seconds. If you do not return 'success' from the server during these 80 seconds, the connection will remain connected until data is returned or the value of' success' is 0. Continue the next request after closing the connection.

$(function () {$("# btn"). Bind ("click", {btn:$ ("# btn")}, function (evdata) {$.ajax ({type: "POST", dataType: "json", url: "data.php", timeout:80000, / / ajax request timeout 80 seconds data: {time: "80"} / / after 40 seconds, the server returns data success:function (data,textStatus) {/ / get the data from the server. Display the data and continue to query if (data.success== "1") {$("# msg") .append ("

[have data] "+ data.text); evdata.data.btn.click ();} / / did not get the data from the server, continue to query if (data.success==" 0 ") {$(" # msg ") .append ("

[numerous data] "); evdata.data.btn.click ();}}, / / Ajax request timed out, continue to query error:function (XMLHttpRequest,textStatus,errorThrown) {if (textStatus==" timeout ") {$(" # msg ") .append ("

[timeout] "); evdata.data.btn.click ();})

PHP

In this case, there is an infinite loop, and the end condition of the loop is to get the return result and return Json data.

And accept the $_ POST ['time'] parameter to limit the timeout of the loop and avoid excessive waste of resources. (browser closes will not send messages to the server, use may continue to cycle)

If (emptyempty ($_ POST ['time']) exit (); set_time_limit (0); / Unlimited request timeout $itimeout; while (true) {/ / sleep (1); usleep (500000); / / 0.5 second $iComplete; / / if you get the data, return the data to the customer server immediately and end this request $rand=rand (1999); if ($rand "1",' name'= > 'xiaocai','text'= > $rand) Echo json_encode ($arr); exit ();} / / server ($_ POST ['time'] * 0.5) tells the customer server that there is no data if ($i==$_POST [' time']) {$arr=array ('success'= > "0",' name'= > 'xiaocai','text'= > $rand); echo json_encode ($arr); exit ();}}

Running effect: it can be seen in the figure that the request time without data reaches 40s, and if the data is obtained in the 40s request, the request is closed.

Thank you for reading this article carefully. I hope the article "how to realize AJAX long polling with the combination of jquery and php" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report