J
J
JackShcherbakov2018-02-17 19:45:04
PHP
JackShcherbakov, 2018-02-17 19:45:04

How to not send a response to a request under certain conditions?

Hello! I have been struggling with this mini-code for several hours now:

<?php

$date = new DateTime();
$minutes = $date->format("i");
if($minutes%2 == 0){
  print "Сервер отправил запрос";
}
?>

The bottom line is that the JS code sends a long request to it and waits for a response. That is, the connection must be kept open until the server responds to the request. But the problem is that as soon as JS sends a request to the server (the code above processes this request), the request immediately sends a response to the request, and accordingly another connection is opened ... As a result, several dozen responses per second are sent to the client. That is, my task is that the server should not respond to the request until new data appears (in my case, this data is an even number of minutes).
How does the script know that the data has arrived?
What can you read to deal with this obscurantism?
Is it even possible to implement this in php?
Everything should work like this -https://learn.javascript.ru/article/xhr-longpoll/l...
Here is the js code:
function subscribe(url) {
  var xhr = new XMLHttpRequest();

  xhr.onreadystatechange = function() {
    if (this.readyState != 4) return;

    if (this.status == 200) {
      console.log(this.responseText);
    } else {
       console.log(this);
    }

    subscribe(url);
  }
  xhr.open("GET", url, true);
  xhr.send();
}
subscribe("http://comet/php.php")

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2018-02-17
@JackShcherbakov

while (true) {}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question