2011-04-25 26 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的^ _^

相關問題