F
F
fLipE2015-05-21 15:20:10
PHP
fLipE, 2015-05-21 15:20:10

PHP does not send a response terminator to a long polling request. what to do?

Given:
NGINX with nginx-push-stream-module for long-polling
Sending a message (php):

$channel_id = '1';
$msg = array('text' => 'a');
$ch = curl_init('http://127.0.0.1/pub?id='.$channel_id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($msg));
if (curl_exec($ch)) 
  echo 'sended '.json_encode($msg);
else 
  echo 'fail';
curl_close($ch);

Receiving messages:
var xhr = new XMLHttpRequest();
xhr.open('GET', '/sub/' + subID, true);
xhr.send();
xhr.onreadystatechange = xhr.onload = function () {
  alert(xhr.readyState);
if (xhr.status === 200) {
  if (xhr.readyState === 4) {
       console.log(xhr.responseText);
}
}
}

Alert shows that when receiving readyState messages, there are only 2 and 3, but not 4.
I just can’t figure out what’s wrong

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question