注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 35岁技术人遭遇年龄坎儿,..
 帮助

关于PHP-Push技术


2007-10-09 18:08:54
 标签:php ajax 无刷新 push   [推送到博客圈]

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

  < 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=img.php >,然后打开你的netscape或其他非ie浏览器查看调用页面,好象没什么变化啊,别急,接着就是怎样变动1.jpg这个图片了,写个另外的php页面来测试吧,比如弄2张图片按时间来覆盖1.jpg(这个方法自己想,用拷贝覆盖也行,只要1.jpg有变化)。这时你就看到调用页面的图片自动更新了。
  使用中你会发现个问题:怎么图片不自动更新了。这是由于客户机在一段时间内没有对服务器发生请求,也就是某一段时间内没有新的内容向浏览器输入,可能发生连接超时现象。什么办法解决呢?可以在执行页面中加个向浏览器发送一个空信号,类似ftp连接方式,上面页面中在do...while(1)间加个print("");
  看完这个例子,应该知道怎么处理不断更新的内容了,关键在img.php页面中的处理(可以是检查新的内容并输出),调用的时候可以有img、script、frame等等。详细的其他使用方面请查看:

  http://www.npds.org/
  应该是法语吧,呵呵。我没学过哦。




    文章评论
 
2008-03-16 13:13:53
下面是想学习中文的外国朋友的情况你想认识他们吗请到雅特士http://www.yateshi.com/community/member_register.php?uid=60439注册 并把此信转发给10位想结交外国朋友的朋友 我会把下面外国朋友的联系方式告诉你和你的朋友他们的联系方式告诉你
!dNt/

Name: Lucy, 15 (female)
From: Worcester, United Kingdom
Language: English, French, chinese, indian
Email:
I WANT CHINESE FRIENDS!
Name: Jephthah Broni, 19 (male)
From: Tema, Ghana
Language: English,  
Email:
I am cool-moving guy.I love making friends.I need friends from China no matter the sex because I will be coming to China in September 2008. Someone who is prepared to teach me Chinese and I promise I will help the person improve rapidly on his or her English.I will be coming to study engineering
My phone number is +233208527412.You can give me a call if you wish
Name: Killinspree, 40 (male)
From: İstanbul, Turkey
Language: Turkish , English
Email:  
It's silly writin' here .This is me and wanna get in touch with you for anything like Chinese,English,culture,people,friendship,countries,music stc..
I Love your culture and country so your people.
Just copy and paste this onto your msn ;)

Jay

Name: Aleksi, 22 (male)
From: Salo, Finland
Language: English, Finnish
Email:  
Hi there! My name is Aleksi and i come from Finland. I would like to have penpals from china, especially from Shanghai because i am coming there in february next year. I work now as a manager in a factory here in Finland. My hobbies are swiming, running and going to the gym. I also watch movies a lot and like to be with my friends. I would be very happy to have chinese penpal who can tell me lot of things in china and the culture too. I am willing to teach english and want to learn chinese myself too. I will look forward to have YOU as my penpal :)
Name: chamal, 22 (male)
From: kandy, Sri Lanka
Language: English, Sinhala
Email:  
Hello every one,my name is chamal I'm 22 years old.I'm from sri lanka I would like to fly and live in china.so any one like to help me please send e-mail to my address
Name: sj, 50 (male)
From: washington DC, United States
Language: English,  
Email:  
I would like to learn Chinese language.
Name: Guillaume, 20 (male)
From: Lyon, France
Language: French, English, Japanese
Email:  
Hi !

I am a student (well, not for long!) in the engineering field and research in computer science. My main interest are artificial intelligence, robotics and programming. I also play a lot of Weiqi (Chinese board game) Don't get scared, I am a pretty nice guy ;p

There are many other things that I like, including cooking (I love spicy food and pastery/cakes !), music (who doesn't ? I am trying to learn Piano in my spare time), japanese dance games such as Dance Dance Revolution or japanese RPG games (it's a nice way to spend time when it's raining outside, isn't it ?) and many other things. Right now, I have a lot of work at school (it's my final year) but I am trying my best to have as much fun as possible ! :)

I am looking for a penpal to help me learn Chinese :) My girlfriend is chinese and I'd be very happy if I could speak with her in her native language ~ I could help you with your english or french ! Anyway, it'd be nice just to talk with you :)

A bientot !

Guillaume

PS : You can contact me directly at


Name: SP CHIN , 37 (male)
From: Kuala Lumpur, Malaysia
Language: Chinese, nglish
Email:
I like to know ladies from China to teach me to write and speak Chinese / mandarin , in return I will teach her in English. If you are interest to learn more in English and my country your reply is most welcome.
Bye! Hape a nice learning



 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: