Jquery+php实现comet(http长连接)

PHP cooljun 2760℃ 0评论

php代码:为了ajax请求超时和是在usleep前加上错误抑制符,或者修改php.ini 的超时时间

  1. <?php 
  2.   
  3. $filename  = dirname(__FILE__).’/data.txt’; 
  4.   
  5. // store new message in the file 
  6. $msg = isset($_GET[‘msg’]) ? $_GET[‘msg’] : ”; 
  7. if ($msg != ”) 

  8.   file_put_contents($filename,$msg); 
  9.   die(); 

  10.   
  11. // infinite loop until the data file is not modified 
  12. $lastmodif    = isset($_GET[‘timestamp’]) ? $_GET[‘timestamp’] : 0; 
  13. $currentmodif = filemtime($filename); 
  14. while ($currentmodif <= $lastmodif) // check if the data file has been modified 

  15.   usleep(10000); // sleep 10ms to unload the CPU 
  16.   clearstatcache(); 
  17.   $currentmodif = filemtime($filename); 

  18.   
  19. // return a json array 
  20. $response = array(); 
  21. $response[‘msg’]       = file_get_contents($filename); 
  22. $response[‘timestamp’] = $currentmodif; 
  23. echo json_encode($response); 
  24. flush(); 
  25.   
  26. ?>

复制代码

html代码:

  1. <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”> 
  2. <html xmlns=”http://www.w3.org/1999/xhtml”> 
  3.   <head> 
  4.     <title>Comet demo</title> 
  5.     <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ /> 
  6.     <script type=”text/javascript” src=”jquery-1.3.2.min.js”></script> 
  7.     <script> 
  8.     var timestamp = 0; 
  9.     var url = ‘backend.php’; 
  10.     var error = false; 
  11.     function connect(){ 
  12.         $.ajax({ 
  13.             data : {‘timestamp’ : timestamp}, 
  14.             url : url, 
  15.             type : ‘get’, 
  16.             timeout : 0, 
  17.             success : function(response){ 
  18.                 var data = eval(‘(‘+response+’)’); 
  19.                 error = false; 
  20.                 timestamp = data.timestamp; 
  21.                 $(“#content”).append(‘<div>’ + data.msg + ‘</div>’); 
  22.             }, 
  23.             error : function(){ 
  24.                 error = true; 
  25.                 setTimeout(function(){ connect();}, 5000); 
  26.             }, 
  27.             complete : function(){ 
  28.                 if (error) 
  29.                     // if a connection problem occurs, try to reconnect each 5 seconds 
  30.                     setTimeout(function(){connect();}, 5000); 
  31.                 else 
  32.                     connect(); 
  33.             } 
  34.         }) 
  35.     } 
  36.     function send(msg){ 
  37.         $.ajax({ 
  38.             data : {‘msg’ : msg}, 
  39.             type : ‘get’, 
  40.             url : url 
  41.         }) 
  42.     } 
  43.     $(document).ready(function(){ 
  44.         connect(); 
  45.     }) 
  46.     </script> 
  47.   </head> 
  48.   <body> 
  49.   
  50.   <div id=”content”> 
  51.   </div> 
  52.   
  53.   <p> 
  54.     <form action=”” method=”get” onsubmit=”send($(‘#word’).val());$(‘#word’).val(”);return false;”> 
  55.       <input type=”text” name=”word” id=”word” value=”” /> 
  56.       <input type=”submit” name=”submit” value=”Send” /> 
  57.     </form> 
  58.   </p> 
  59.   
  60.   </body> 
  61. </html>

复制代码

但是光这样是不足以投入使用的,在测试中本人发现两个问题:

1. HTTP 1.1规定客户端不应该与服务器端建立超过两个的HTTP连接,新的连接会被阻塞。而IE系列8.0以下版本的浏览器就是这么做的,所以若同时打开两个浏览器窗口(IE8.0以下)或者是刷新了页面,程序就死掉了。

2. 通常情况下web服务器允许脚本最大执行时间是30秒,所以要让程序正常运行,就需要将服务器的脚本最大执行时间设置成无限,但似乎也不是上上策。

但事实证明php是可以很好地实现服务器推技术的,如phpfreechat即时聊天室,开心网和白社会的即时消息都是成功案例(它们都不会出现上面的两个问题)。

转载请注明:cooljun小窝 » Jquery+php实现comet(http长连接)

如果你觉得这篇文章对你有帮助,请支持我继续更新网站 !捐赠本站
喜欢 (0)or分享 (0)

您必须 登录 才能发表评论!