0

出于某种原因,对新聊天消息的检查会导致浏览器(在某种程度上是服务器)负载比我预期的要大。任何人都可以看到我可以提高效率以减轻负载的任何方法吗?

// Begin the cycle of refreshing the mini chat after the standard delay.
function startRefreshingMinichat(){
    var secs = 30; // Chat checking frequency.
    setTimeout(function (){
        checkForNewChats();
        startRefreshingMinichat(); // Loop the check for refresh.
    }, secs*1000);
}

// Check for the latest chat and update if it's different.
function checkForNewChats(){
    // Check whether the latest chat doesn't match the latest displayed chat.
    // NOTE THAT THIS CALLBACK DOES NOT TRIGGER IMMEDIATELY.
    $.getJSON('api.php?type=latest_chat_id&jsoncallback=?', function(data){
        var newChats = false;
        // Update global data stores if an update is needed.
        if(updateDataStore(data.latest_chat_id, 'chat_id', 'latestChatId', 'chat_id')){
            newChats = true;
        }
        if(newChats){ // there are new chats to show.
            refreshMinichat(null, 50); // loads new chat content.
        }
        // Since this callback isn't immediate, any feedback has to occur whenever the callback finishes.
 }); // End of getJSON function call.
}
4

2 回答 2

1

查看CometD。这是一个 js 长轮询系统,我在与 jQuery 集成的简单聊天系统中取得了一些成功。(上次我查看时,有一些 jQuery 特定的实现,但我从来没有找到一个对我来说足够健壮的实现。)

于 2010-05-06T13:18:29.300 回答
1

您可以签出这个推送引擎,这样您就不必再轮询新数据了。检查一下,它真的很酷。

于 2010-05-06T13:06:06.067 回答