喜欢用Facebook的AJAX聊天应用程序应用程序、喜欢、Facebook、AJAX

2023-09-10 21:52:20 作者:踏骨征王

我要开发像Facebook聊天应用。我这样做,现在它工作正常。我用AJAX继续服务器请求保存和检索数据。一个函数被调用每次10秒:

I want to develop a chat application like facebook. I did this and now it works fine. I used ajax for continuing server request to save and retrieve data. One function which is called each 10 second:

// Load  message
(function loadClient() {
    $.ajax({
        type: 'POST',
        data: 'c_id=' + $.cookie("c_id") + '&offset=' + $('#c_name_msgHead').data('offset'), //'foo='+ bar+'&calibri='+ nolibri,
        dataType: 'json',
        url: $("#webroot").text() + 'chats/loadMsg',
        success: function (data) {
            var id =0;
              if ($.cookie("status") == "active"){

            $.each(data, function (i, item) {

                    if(item.Chat.status == 'active'){
                        $('.temp_msg').remove();
                    }

                    if (!$('#' + item.Chat.id)[0]) {
                        if (item.Chat.admin_message) {
                            $('<div class="msg_b" id="' + item.Chat.id + '">' + item.Chat.admin_message + '</div>').insertBefore('.client_area .msg_push');
                        }
                        if (item.Chat.client_message) {
                            $('<div class="msg_a" id="' + item.Chat.id + '">' + item.Chat.client_message + '</div>').insertBefore('.client_area .msg_push');
                        }
                        $('.msg_body').scrollTop($('.msg_body')[0].scrollHeight);
                    }


                 id = item.Chat.id;
            });

            $('#c_name_msgHead').data('offset', id);

              } 


        },
        complete: function () {
            // Schedule the next request when the current one's complete
            setTimeout(loadClient, 3000);
        }
    });
})();
// END load message

这在10秒之内载入更新数据。现在,如果有10000用户同时10000请求将被发送到我的服务器,它是一个关注的性能和服务器停机可能会发生。即使万用户未开始交谈10000请求将被执行。所以,我应该怎么办开发这样的应用程序,不断需要老虎服务器或技术用于Facebook的聊天。任何想法将AP preciated。谢谢

It load update data after 10 second. Now if there are 10000 users at a time 10000 request will be send to my server which is a concerned of performance and shutdown of server may occur. Even if 10000 users did not start chatting 10000 request will be performed. So what should I do to develop such application which need to tiger server continuously or which technology is used for facebook chatting. Any idea will be appreciated. Thanks

推荐答案

Facebook正在利用所谓的技术的长轮询。 然而,对于聊天室具有高响应速度,强烈建议和更好的使用socket.io用的node.js作为服务器侧使用的插座,实现了客户机和服务器之间的大多数的实时双向通信信道。

Facebook is using the technique called Long Polling. However,for chatroom with high respond rate, it is strongly suggested and much better to use socket.io with node.js as your server side which makes use of sockets to achieve most realtime bi-directional communication channel between a client and a server.

您可以阅读下面的教程为你的起点

You can read the following tutorial as your starting point

http://socket.io/get-started/chat/