【web】http长连接技术(2)http push的php实现

   标签:web php push

这种方式实际上是长连接的最简单实现。和cgi很类似。

PHP实现:
主要是想演示一下几点:
set_time_limit(0);
这个用来保证长连接不会因为php请求超时而中断
   ob_flush();
   flush(); // used to send the echoed data to the client
这两句共同保证php会即时清空 缓冲并输出
等效于perl中的$|=1
sleep(1);
延时1s.这个太重要了。
用于循环时释放cp
javascript和VB 都没有这个功能。
相关源码:
<?php

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 2008 05:00:00 GMT");
flush();

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Comet php backend</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>


<?php
set_time_limit(0);
while(1) {
  echo '<span>'.time().'<BR></span>';
  ob_flush();
  flush(); // used to send the echoed data to the client
  sleep(1); // a little break to unload the server CPU
}

?>
</body>
</html>
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值