comet 框架 php,简单PHP Comet示例

This is a simple iframe-based PHP Comet example. Additional work must be done to be able to build up and tear-down connections, but the effect is solid. This is a quick and dirty method without having to use heavy feature detection to determine if xhr readystate 3 is available.

frontEnd.html:

Comet demo

.hiddenIframe {

position:absolute;

top:0px;

left:0px;

display:block;

z-index:-999;

visibility:hidden;

}

The server time will be shown here

var content = document.getElementById('content');

var dumpText = function(text){

content.innerHTML = content.innerHTML + '
'+ text;

}

backEnd.php:

/*

Simple COMET script tested to work with IE6, IE8, IE9, Chrome 5, Chrome 10, Firefox 3.6.16, Firefox 4, Safari 5, Opera 11

*/

header("Cache-Control: no-cache, must-revalidate");

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

//Turn of Apache output compression

// Necessary if you have gzip setup in your httpd.conf (e.g. LoadModule deflate_module modules/mod_deflate.so)

apache_setenv('no-gzip', 1);

ini_set('zlib.output_compression', 0);

//Disable all PHP output buffering

ini_set('output_buffering', 'Off');

ini_set('implicit_flush', 1);

ob_implicit_flush(1);

for ($i = 0, $level = ob_get_level(); $i < $level; $i++) { ob_end_flush(); } //Flush all levels of the buffer to start

error_reporting(E_ALL);

?>

Comet php backend

var dumpText = window.parent.dumpText;

$startTime = time();

$maxLoopTime = 10;

while(time()-$startTime < $maxLoopTime) {

echo '

echo "dumpText(".time().');'.str_repeat(' ',500); //500 characters of padding

echo "\r\n";

flush();

$randSleep = mt_rand(100000, 2000000); //sleep between 100 ms and 2 seconds

usleep($randSleep);

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值