|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
会HTML吗?会,我能编好几个大表格排板的网页啦!刷新 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>
培训的第一阶段,学习的是HTML/CSS/JavaScript基础。 |
|