Javascript/php implements comet (Ajax long polling) push function

time:15-11-16 forum:PHP tools and code
  http://blog.csdn.net/hhq767063071/artic... Ls/8882577

simple description:

long polling process: the page initiates a server request, and then the server keeps connecting until the data returns. After the data is returned, the browser closes the connection and then another server request is launched. This process is maintained continuously during page opening.

[html] view plaincopy
[code]
.Lt; input id=.quot; btn.quot; type=.quot; button.quot. (empty ($_POST.#91;'time'.#93;)) exit ();
set_time_limit (0); / / / unlimited request timeout
$i=0;
while (true) {
{1; 500000 / > > > > > > > / / if the data is obtained, the data is returned to the customer service side immediately, and the request is
$rand=rand (1999);
if ($rand.lt; =900) {
$arr=array ('success'=.gt;.Quot; 1.quot;'name'=.gt; > > / / /); _encode ($arr);
echo $rand;
Exit ();
}

/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / server ($_POST.#91;'time'.#93;) Rray ('success'=.gt;.Quot; 0.quot;'name'=.gt;'xiaocai','text'=.gt; $rand);
//echo json_encode ($arr); Ript] view plaincopy

[code] window.onload=function () {
var BTN = document.getElementById (.Quot; btn.quot;); Br / > console.log (.Quot; click.quot;);
var XHR = new XMLHttpRequest ();
xhr.onreadystatechange = function () {= = = 4) {{200 / > 300). Us = = = 304) {
msg.innerHTML = xhr.responseText;
event.target.click ();
}
else{
alert (xhr.status); {} / >} LearTimeout (ID);
}
}
xhr.open (.Quot; post.quot;.Quot; data.php.quot; true); {
xhr.abort ();
alert (.Quot; timeout.quot;);
}, 80000);
xhr.send (.Quot; time=80.quot;);
} / > > because of the discovery of the base network in the learning process. The data is implemented using plug-ins (jQuery, etc.), which is implemented in pure JS and is worth collecting.

prev:Using Json to realize data exchange betw... next:A brief discussion on comet Technology
post comment
Notice



approved   2017 SONY children's life is a conce...   Massaging the acupoints can relieve ...   Baby swimming pool built in Britain   The survival record of women scienti...   The Russian 4 year old girl walked 3...
rand   PHP verification code landing check   Install.php simple production   Wan net interview PHP written test   PHP automatically identifies links i...   PHP determines whether or not the us...
ads

other

Because all the contents of this site are reproduced from other media, the intention is to provide free services to the public, but it does not mean that this site agrees with its views, and can not be responsible for the authenticity of its verification, such as the copyright unit or individual of the manuscript does not want to publish on this site, please Contact me, I will immediately remove it, thank you. Contact: atseashawk@163.com