Home > php教程 > php手册 > PHP-Push技术实现刷新功能

PHP-Push技术实现刷新功能

WBOY
Release: 2016-06-21 09:15:32
Original
1084 people have browsed it

刷新

Server push 前一段时间炒得很热的“推”技术,不过网上大部分都是cgi的资料,偶尔看到一个法国的网站上有这么个介绍,可惜法语看不懂,只能从他的程序中看懂点东西,现整理个例子出来大家学习一下。可以用于聊天室的数据传输、网站上的新闻更新、等等各类更新频繁的页面。

以前做刷新主要通过页面上加标签。

 



或者使用javascript的timeout+reload,不过这种刷新的方法取决于时间的设定,无法连续的数据传输且时间不好确定。采用了Server push的服务器在客户机做出一个请求后,和客户机建立一个永久的连接,然后服务器会根据客户机的请求不断地把数据包推向服务器。那些你觉察不到的延迟会让你觉得服务器的响应和你的请求已经达到了同步的程度。

先来看一下例子再解释。

 img.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);
}
? >




这就是一个永久执行的页面(网络不断的情况下),不断输出图片的内容,下面是调用的页面。PHP-Push技术实现刷新功能,然后打开你的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:

PHP-Push技术实现刷新功能

Use this bit of PHP on the page that references the image to compensate for IE's lack of "innovation":



if (ereg("MSIE",$HTTP_SERVER_VARS["HTTP_USER_AGENT"])) {
echo "\n";
}
?>




Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template