polling

header("Expires: Sun, 19 Nov 1978 05:00:00 GMT");
header("Last-Modified: ". gmdate("D, d M Y H:i:s") ." GMT");
header("Cache-Control: store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", FALSE);
 
$msg = get_msg();
if ($msg) {
    echo $msg;
} 
else {
    echo '0';
}

long polling

header("Expires: Sun, 19 Nov 1978 05:00:00 GMT");
header("Last-Modified: ". gmdate("D, d M Y H:i:s") ." GMT");
header("Cache-Control: store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", FALSE);
//在$timeout之后,关闭连接,并且要求客户3秒后重新请求
for ($i = 0, $timeout = 60; $i < $timeout; $i++ ) {
 $msg = get_msg();
 if ($msg) {
     echo json_encode(array('t' => 'info' , 'c' => $msg));
     flush();
     exit(0);
 }  
 usleep(3000000);
}
echo json_encode(array('t' => 'refresh', 'c' => 3000));
flush();

简单说明long polling 和 polling的区别


phoenixwu0229
185 声望5 粉丝

引用和评论

0 条评论