11

I am following this example for Spring MVC chat client which used HTTP long polling.

My web server is located at port 7555, and I need to be able to make an HTTP long polling request to port 7555 from port 80 (browser) so I created a PHP script that calls my webservice.

<?php
$index = $_GET["index"];
echo $index;
echo $index2;

$urlVar = "http://localhost:7555/test?" . $index . $index2;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlVar);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PORT, 7305);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_exec($ch)
?>

I call this PHP file from my JavaScript with parameters like this:

($.ajax({
    url : "http://localhost/myphpscript.php?index=" + i, 
    type : "GET", 
    cache: false,
    success : function(messages) {
       //do stuff
    }
}));

The PHP file is located is located in my localhost. This does not seem to work because the JavaScript seems to calling the PHP (which calls the URL) endlessly. Am I doing long polling correctly with PHP curl? Do I need to make the Ajax call in JavaScript since I am the HTTP call in curl?

miken32
  • 42,008
  • 16
  • 111
  • 154
Jonatha Suh
  • 195
  • 13

2 Answers2

1

With CURLOPT_RETURNTRANSFER you'll need to echo the results of curl_exec($ch)

echo curl_exec($ch);
Tom
  • 4,257
  • 6
  • 33
  • 49
Jason Hendry
  • 193
  • 8
0

Because it's not allowed to send cross site requests (this holds for ports as well) you need to do this PHP relais thing.

Never the less. Requesting the same request over and over again (polling) is almost right BUT your web-service should keep the connection open until it has some new information or the request times out (long polling).

What does your web service return (Http-Status ok? Any content?)

Community
  • 1
  • 1
Inceddy
  • 760
  • 1
  • 6
  • 18
  • it doesn't long poll, it just keeps calling the server over and over again in an infinite loop. Does work as intended just calls the php (which calls the server) over and over over again – Jonatha Suh Jul 20 '15 at 18:04
  • 1
    So you say it's a problem with your JS? But then please update your question with the full JS code. As I understand long polling: JS querys your PHP which querys your webservice. The webservice keeps your connection open to the PHP/Apache server which then keeps his connection open to your Browser request. So the only reason that its calling (fast) over and over again is, that someone in this chain is not keeping his connection open. – Inceddy Jul 21 '15 at 06:53