Problem with access limit by refresh account JAVASCRIPT

2

Is there any way to return updated direct server data in HTML without using setInterval , setTimeout these things?

Because these functions are getting rid of the server .. Every time it refresh it is as if it were having one more access of a user in the site, which ends up breaking the limit of daily accesses in the server.

Example:

    var loop = setInterval;

    function setLoop(de, para, nome) {
       var url_s = $("#url_s").val();
       // $("._5chat").attr('id','chat_'+para);

       clearInterval(loop);
       loop = setInterval(function() {
           $("#mensChat div._5chat").load(url_s+'/demo/chat/test.php?de='+de+'&para='+para+'&url_s='+url_s)
       }, 1000);
    }

    $(document).ready(function()
    {
        $('.usuario').click(function() {
            setLoop($(this).attr('user-d'), $(this).attr('user-p'), $(this).attr('nome-p'));
        });
    });

When it takes a refresh in 1000ms of time setInterval it is as if the server received another visitor ...

Two people in 1hr on the site think it is enough to exceed the limit now imagine the world ...

    
asked by anonymous 14.07.2015 / 16:06

2 answers

4

Each HTTP request is a new request to the server. It is only possible to "simulate" sessions thanks to the server-side languages that support this, as is the case with PHP. But this is only a "current session" request for PHP, the apache server interprets it as a new request and has no "clue" that user was already "using" the services.

So two people using the site for two hours with the refresh of 1 second, for the server will be 14400 independent requests and there is nothing that can be done about it except the following solutions:

  • Increase the request time (5s would be fine, but it depends on your need, in case a chat is not viable);
  • Hire a hosting that has unlimited traffic. (Indispensable).

An interesting observation is that DoS attacks are quite similar to what they were doing, except that they see in a much larger number of requisitions in a much shorter period of time. In other words, your own site was "attacking" and the reason comes next.

Another problem is the use of the setInterval function that sends a new request before the same one as the previous one ends (in the case of asynchronous requests). Create a recursive function using setTimeout as already quoted in the comments by Guilherme Nascimento .

AtualizaDados = function(){
  var endereco = 'https://fiddle.jshell.net/';
  var params = {param1: 1, param2: 2};
  var time = 3000;
  var el = $("#mensChat div._5chat");

  $.ajax({
    url: endereco,
    type: 'GET',
    dataType: 'html',
    data: params,
    success: function(html){
      el.html(html);
    },
    error: function(x, s){
      console.log(s, x);
    },
    complete: function(){
      setTimeout(AtualizaDados, time);
    }
  });
};

AtualizaDados();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><divid="mensChat">
    <div class="_5chat"></div>
</div>

A good practice is also to reduce the amount of data to be trafficked, that is, remove the "junk" from your requests, start using JSON to traffic the data, instead of downloading the HTML content they would have for example of 300 to 500 bytes (each character is a , including whitespace ) would have only about 80 to 120 bytes (depending on message size), and then mount the HTML on the client side, for example:

Server:

<?php

  /* Executa todas as ações necessárias */

  header('Content-Type: application/json');
  echo json_encode(Array(
     'autor' => $autor,
     'msg'   => $msg,
     'time'  => $time,
     'etc'   => $etc
  ));
  exit;

Client:

<script type="text/javascript">

AtualizaDados = function(){
  var endereco = 'http://endereco/da/application/';
  var params = {param1: 1, param2: 2};
  var time = 3000;
  var el = $("#mensChat div._5chat");

  $.ajax({
    url: endereco,
    type: 'GET',
    dataType: 'json',
    data: params,
    success: function(json){
      el.append(
        $('<div />').addClass('mensagem')
                    .append( $('<span />').addClass('autor').text( json.autor ) )
                    .append( $('<span />').addClass('msg').text( json.msg ) )
                    .append( $('<span />').addClass('hora').text( json.time ) );
      );
    },
    error: function(x, s){
      console.log(s, x);
    },
    complete: function(){
      setTimeout(AtualizaDados, time);
    }
  });
};

AtualizaDados();
    
14.07.2015 / 16:18
3

As I said in the other answer, setInterval does not wait for the load to finish, then use setTimeout : Upload notifications ChatBox message

function setLoop(de, para, nome) {
   var url_s = $("#url_s").val();

   $("#mensChat div._5chat").load(url_s+'/demo/chat/test.php?de='+de+'&para='+para+'&url_s='+url_s,
       function() {
            setTimeout(function() {
                setLoop(de, para, nome);
            }, 1000);
       }
   );
}

$(document).ready(function() {
    $('.usuario').click(function() {
        setLoop($(this).attr('user-d'), $(this).attr('user-p'), $(this).attr('nome-p'));
    });
});
    
14.07.2015 / 16:17