PHP-Push技術實現刷新功能
2024-05-04 22:53:41
供稿:網友
server push 前一段時間炒得很熱的“推”技術,不過網上大部分都是cgi的資料,偶爾看到一個法國的網站上有這么個介紹,可惜法語看不懂,只能從他的程序中看懂點東西,現整理個例子出來大家學習一下??梢杂糜诹奶焓业臄祿鬏?、網站上的新聞更新、等等各類更新頻繁的頁面。
以前做刷新主要通過頁面上加標簽。
< meta http-equiv=refresh content="time;url=url" >
或者使用javascript的timeout+reload,不過這種刷新的方法取決于時間的設定,無法連續的數據傳輸且時間不好確定。采用了server push的服務器在客戶機做出一個請求后,和客戶機建立一個永久的連接,然后服務器會根據客戶機的請求不斷地把數據包推向服務器。那些你覺察不到的延遲會讓你覺得服務器的響應和你的請求已經達到了同步的程度。
先來看一下例子再解釋。
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");
//這里是關鍵哦,看看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);
}
? >
這就是一個永久執行的頁面(網絡不斷的情況下),不斷輸出圖片的內容,下面是調用的頁面。<img src=http://www.163design.net/p/b/img.php>,然后打開你的netscape或其他非ie瀏覽器查看調用頁面,好象沒什么變化啊,別急,接著就是怎樣變動1.jpg這個圖片了,寫個另外的php頁面來測試吧,比如弄2張圖片按時間來覆蓋1.jpg(這個方法自己想,用拷貝覆蓋也行,只要1.jpg有變化)。這時你就看到調用頁面的圖片自動更新了。
使用中你會發現個問題:怎么圖片不自動更新了。這是由于客戶機在一段時間內沒有對服務器發生請求,也就是某一段時間內沒有新的內容向瀏覽器輸入,可能發生連接超時現象。什么辦法解決呢?可以在執行頁面中加個向瀏覽器發送一個空信號,類似ftp連接方式,上面頁面中在do...while(1)間加個print("");
以上是轉的部分,由于比較有興趣,在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>