0

jquery ajaxを使用した自動リクエストがあります。この機能を使用して、新しいチャットメッセージと通知ケースを検出しています。クライアントの自動リクエストが終了しない場合の影響をもう一度考えていることがありますが、

これは DDOS HTTP スロットリングのようなものだと思うので、サーバーがダウンしているのではないかと心配しています。

これは私のコードです

    $(function(){
         initChat();
    });

    /* 
     * initialize chat system
     */
    function initChat() {
        setTimeout("notifChat()" ,2000);    
    }

    function notifChat() {
        $.ajax({
            url: '/url',
            type:"GET",
            data: {id:$("#id").val()},
            success:function (data,msg) {
                //to do success

            }
        });
        setTimeout("notifChat()" ,2000);
    }

私の質問は

  1. サーバーをダウンさせたり、サーバーをハングアップさせたりすることはできますか?
  2. それがより良い考えではない場合、何らかの提案はありますか?
4

1 に答える 1

1

注: これは本番環境向けのコードではありません。テストは行っていません。

このコードの数週間:

2 つの http 接続制限を処理しません

強み:

サーバーがエラーを返すかどうかがわかります (サーバーエラー 404,403,402 など)。

var failed_requests = 0;
var max = 15;

$(function(){

     initChat();
});

/* 
 * initialize chat system
 */
function initChat()
{
     setTimeout(
             function() 
             {
                notifChat(); 
             }, 2000)
}


function notifChat() {
    $.ajax({
        url: '/url',
        type:"GET",
        data: {id:$("#id").val()},
        success:function (data,msg) 
        {
            //to do success

        },
        complete: function()
        {

            // either call the function again, or do whatever else you want.


        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {   
            failed_requests = failed_requests + 1;

            if(failed_requests  < max)
            {
                setTimeout(
                         function() 
                         {
                            notifChat();
                         }, 2000)
            }
            else
            {  
                alert('We messed up');
            }

        }


    });

}
于 2012-08-10T06:39:09.977 に答える