2011-04-25 27 views
4

我以前见过这个问题,并且在http://www.zeitoun.net/articles/comet_and_php/start中发现了这个例子,这个例子非常好,很清楚。但是,它使用JavaScript。在Apache上有没有任何jQuery函数或插件?

我的问题是,有没有任何插件,函数或什么东西可以帮助我轻松实现jQuery的PHP彗星?因为给出的例子需要大量的JavaScript代码。

顺便说一句,我想在Apache上使用它。可能吗?

+0

Apache的不旨在使使用彗星非常容易。请参阅[这个问题](http://stackoverflow.com/questions/603201/using-comet-with-php)了解更多详情。 – justkt 2011-04-25 14:37:09

+0

@justkt Apache确实有彗星支持,请参阅[NIO](http://tomcat.apache.org/tomcat-7.0-doc/aio.html)。但我会同意这不是很容易。 – Andrew 2011-04-25 15:16:25

+1

你可能想选择一个答案,或者你可能已经失去了50代表^ _ ^ – Neal 2011-05-06 16:04:52

回答

3

如果你只是做长轮询然后jQuery将正常工作。但是,jQuery不公开readyState === 3事件,所以没有内置的方式来获取数据,因为它是流式传输,如果这是你想要的方向。

[编辑] 这里是错误,#1172

它看起来就像他们在1.5中加入的功能,使用Prefilter

所以,是的,你可以做所有彗星的东西用jQuery现在:)

3

彗星是长轮询,客户端发送请求并等待来自服务器的响应。服务器对请求进行排队,一旦获得更新结果。它将响应发送给客户端。

所以基本上你只需要发送一个.ajax请求到服务器并使用回调来处理返回的数据。除非服务器获取更新的数据,否则将不会调用onSuccess回调。

客户端没什么特别的感觉。实际的游戏是在服务器端排队请求,然后做出相应的响应。

看看这个答案详细的代码示例>How do I implement basic "Long Polling"?

3

我已经彗星之前的jQuery的版本,这是我做了什么:

var comet = { 
    connection : false, 
    iframediv : false, 

    initialize: function(){ 
     // For other browser (Firefox...) 
     comet.connection = $('<iframe>'); 
     comet.connection.attr('id', 'comet_iframe'); 
     comet.connection.css({ 
      left  : "-100px", 
      top  : "-100px", 
      height  : "1px", 
      width  : "1px", 
      visibility : "hidden", 
      display : 'none' 
     }) 
     //comet.iframediv = $('<iframe>'); 
     comet.connection.attr('src', 'backend.php'); 
     //comet.connection.append(comet.iframediv); 
     $('body').append(comet.connection); 
    }, 
    // this function will be called from backend.php 
    printServerTime: function (time) { 
     console.log('time',time); 
     $('#content').html(time); 
    }, 

    onUnload: function() { 
     if (comet.connection) { 
     comet.connection = false; // release the iframe to prevent problems with IE when reloading the page 
     } 
    } 
    } 
    $(window).load(comet.initialize) 
      .unload(comet.onUnload); 

我采取了正确的代码关闭该页面,并使其jQuery的^ _^

相关问题