検索
ホームページphp教程PHP源码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(&#39;no-gzip&#39;, 1);
ini_set(&#39;zlib.output_compression&#39;, 0);

//Disable all PHP output buffering
ini_set(&#39;output_buffering&#39;, &#39;Off&#39;);
ini_set(&#39;implicit_flush&#39;, 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 &#39;<script type="text/javascript">&#39;;
	echo "dumpText(".time().&#39;);&#39;.str_repeat(&#39; &#39;,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>


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(&#39;content&#39;);
	var dumpText = function(text){
		content.innerHTML = content.innerHTML + &#39;<BR>&#39;+ text;
	}
</script>
<iframe src="comnet.php"></iframe>
</body>
</html>

                               


                   

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター