COMET基于HTTP长连接的推送
COMET基于HTTP长连接的推送
比如web IM 消息比轮询即时。减轻服务器压力。减少TCP三次握手的次数
1. [文件]
comnet.php
<?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); ?><html> <head> <title>Comet php backend</title> </head> <body> <script type="text/javascript"> var dumpText = window.parent.dumpText; </script> <?php $startTime = time(); $maxLoopTime = 100; while(time()-$startTime < $maxLoopTime) { echo '<script type="text/javascript">'; echo "dumpText(".time().');'.str_repeat(' ',500); //500 characters of padding echo "</script>\r\n"; flush(); $randSleep = mt_rand(100000, 2000000); //sleep between 100 ms and 2 seconds usleep($randSleep); } ?> <script type="text/javascript">dumpText("Output Finished");location.reload();</script> </body> </html>
Copy after login
2. [文件] in.php
<html> <head> <title>Comet demo</title> <style type="text/css"> .hiddenIframe { position:absolute; top:0px; left:0px; display:block; z-index:-999; visibility:hidden; } </style> </head> <body> <p id="content">The server time will be shown here</p> <script type="text/javascript"> var content = document.getElementById('content'); var dumpText = function(text){ content.innerHTML = content.innerHTML + '<BR>'+ text; } </script> <iframe src="comnet.php"></iframe> </body> </html>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
4 weeks ago
By DDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
3 weeks ago
By DDD
Where to find the Crane Control Keycard in Atomfall
4 weeks ago
By DDD
Roblox: Dead Rails - How To Complete Every Challenge
1 months ago
By DDD
How to fix KB5055523 fails to install in Windows 11?
2 weeks ago
By DDD

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
