PHP-Push技術(shù)實現(xiàn)刷新功能
2024-05-04 22:53:41
供稿:網(wǎng)友
 
server push 前一段時間炒得很熱的“推”技術(shù),不過網(wǎng)上大部分都是cgi的資料,偶爾看到一個法國的網(wǎng)站上有這么個介紹,可惜法語看不懂,只能從他的程序中看懂點東西,現(xiàn)整理個例子出來大家學(xué)習(xí)一下。可以用于聊天室的數(shù)據(jù)傳輸、網(wǎng)站上的新聞更新、等等各類更新頻繁的頁面。 
以前做刷新主要通過頁面上加標(biāo)簽。 
  
< meta http-equiv=refresh content="time;url=url" >
或者使用javascript的timeout+reload,不過這種刷新的方法取決于時間的設(shè)定,無法連續(xù)的數(shù)據(jù)傳輸且時間不好確定。采用了server push的服務(wù)器在客戶機做出一個請求后,和客戶機建立一個永久的連接,然后服務(wù)器會根據(jù)客戶機的請求不斷地把數(shù)據(jù)包推向服務(wù)器。那些你覺察不到的延遲會讓你覺得服務(wù)器的響應(yīng)和你的請求已經(jīng)達(dá)到了同步的程度。 
先來看一下例子再解釋。 
 img.php
  < ?php
    set_time_limit(0);
    $file = "./1.jpg";
    $sep = "girlskickassitsayssoonatshirt";
  if(ereg(".*msie.*",$http_server_vars["http_user_agent"])){
  //如果是ie瀏覽器,直接輸出就退出,ie的不支持哦,我沒試出來過
    header("cache-control: no-cache");
    header("pragma: no-cache");
    header("content-type: image/jpeg");
    header("content-size: " . filesize($file));
    readfile($file);
  }else{
    header("content-type: multipart/x-mixed-replace; boundary=$sep");
  //這里是關(guān)鍵哦,看看mime類型說明
  //你會明白
  print "--$sep
";
  do{
    print "content-type: image/jpeg
";
    readfile($file);
    print "
--$sep
";
    flush();
    $mt = filemtime($file);
    do{
      sleep (1);
      clearstatcache();
     }while($mt == filemtime($file));
  }while(1);
}
? >
這就是一個永久執(zhí)行的頁面(網(wǎng)絡(luò)不斷的情況下),不斷輸出圖片的內(nèi)容,下面是調(diào)用的頁面。<img src=http://www.163design.net/p/b/img.php>,然后打開你的netscape或其他非ie瀏覽器查看調(diào)用頁面,好象沒什么變化啊,別急,接著就是怎樣變動1.jpg這個圖片了,寫個另外的php頁面來測試吧,比如弄2張圖片按時間來覆蓋1.jpg(這個方法自己想,用拷貝覆蓋也行,只要1.jpg有變化)。這時你就看到調(diào)用頁面的圖片自動更新了。 
使用中你會發(fā)現(xiàn)個問題:怎么圖片不自動更新了。這是由于客戶機在一段時間內(nèi)沒有對服務(wù)器發(fā)生請求,也就是某一段時間內(nèi)沒有新的內(nèi)容向瀏覽器輸入,可能發(fā)生連接超時現(xiàn)象。什么辦法解決呢?可以在執(zhí)行頁面中加個向瀏覽器發(fā)送一個空信號,類似ftp連接方式,上面頁面中在do...while(1)間加個print(""); 
以上是轉(zhuǎn)的部分,由于比較有興趣,在google上找了一下,大家可以看看下面的資料.
requirements 
works with apache-1.3.14/php4.0.3pl1 server and various netscape clients. probably many other server combos. tested on netscape 4.7x and 6.0/mozilla. 
does not work with ie. internet exploiter does not support x-mixed-replace server-push as far as i know. if a browser has "msie" in its user-agent string the script will display one image and exit. 
update 20020108: poked around freshmeat for a bit and found andy wilcock's cambozola java applet which seems to work well with my php script to make the stream viewable under ie. beware that the current version doesn't work under "name-based" virtual hosts but i'll have a patch for it soon.
source
download 
<?
$file = "./latest.jpg";
$sep = "girlskickassitsayssoonatshirt";
if (ereg(".*msie.*",$http_server_vars["http_user_agent"]))
{
# if ie, spit out one pic and exit
header("cache-control: no-cache");
header("pragma: no-cache");
header("content-type: image/jpeg");
header("content-size: " . filesize($file));
readfile($file);
}
else
{
# if not ie, give the browser a try
header("content-type: multipart/x-mixed-replace; boundary=$sep");
print "--$sep/n";
do {
print "content-type: image/jpeg/n/n";
readfile($file);
print "/n--$sep/n";
flush();
$mt = filemtime($file);
do {
sleep (1);
# we won't output the same image twice.
clearstatcache();
} while ($mt == filemtime($file));
} while (1);
}
?>
make sure there are no blank lines outside the <? ?> in your script. that will cause screwey headers to be sent too soon.
reference the script in your html page as if it was an image: 
<img src="server-push.php" height=240 width=320> 
use this bit of php on the page that references the image to compensate for ie's lack of "innovation": 
<head>
<?
if (ereg("msie",$http_server_vars["http_user_agent"])) {
echo "<meta http-equiv=/"refresh/" content=/"4;/">/n";
}
?>
</head>