D
D
daminik002016-06-19 02:12:16
PHP
daminik00, 2016-06-19 02:12:16

How to competently organize a page refresh to send a request?

There is a page on which, using php, a code is written that checks the last message in VK and responds to it under certain conditions. What is the best way to organize a page update, since the code only works when the page is updated. It is necessary that the site is somehow updated in the background if a new message arrives. The main thing is that there would not be a large load on the server. You can not paint the solution completely, but simply direct it in the right direction. Thank you very much in advance)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Daemon23RUS, 2016-06-19
@Daemon23RUS

Fragment HTML

<body onload="(function() {getVSD();window.setInterval(getVSD, 3000)})()">

In addition to it, a function in Java
function getVSD(){			//Get Status of Servers
  var xmlhttp = getXmlHttp();
  xmlhttp.open('GET', 'status.php', true);

  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
      if(xmlhttp.status == 200) {
        var response = JSON.parse(xmlhttp.responseText);
        response.forEach(function(item){loadContent(item.element,item.status);});
      } else {GLOBAL_STATUS.forEach(function(item){loadContent(item.element,item.status);});}
    };}
  xmlhttp.send(null);
}

If my memory serves me, then this bundle will poll status.php once every 3 seconds. This is a fragment from a working project, so finish it for yourself ..
In my case, with a successful request ( if(xmlhttp.status == 200)), the corresponding info on the page changes without reloading the page as such.

R
Rou1997, 2016-06-19
@Rou1997

To get a message from the server without refreshing the page, it's enough to make an AJAX request (HTTP request using JavaScript), you can start with jQuery or from here , but in your case you will have to send requests very often, this is a big load on both the client and the server, you can use long polling instead of frequent, but WebSockets is better, it's almost "raw" TCP / IP, unlike HTTP, you don't have to send requests (packets) at all, so there will be no load on the client and server until the server itself sends a message .

P
profesor08, 2016-06-19
@profesor08

Try using a widget. Or are there none?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question