0

I have a PHP Class to push message with socket like :

function __construct()
{
     $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, 0); //I tried with 99999 for timeout too
}

function push($params)
{
     $req = 'GET /push?'.$params." HTTP/1.1\r\n"
            . 'Host: '.$this->host."\r\n"
            . "Content-Type: application/x-www-form-urlencoded\r\n"
            . 'Content-Length: '.strlen($params)."\r\n"
            . "Connection: keep-alive\r\n\r\n";

     fwrite($this->socket, $req);
}

But if I tried to push 2 or more message, only one is receive by NodeJS serveur :

foreach(['foo=2', 'bar=42'] as $loop)
{
   $pusher->push($loop);
}

Now, if i put this line $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, 0); just before fwrite, all messages will be send...

So why I can't use only one connexion for multiple request with same soket?

I use "keep-alive" and I don't call fclose, so I don't understand why my nodeJS server receive only one message in first case...

9
  • Are you certain that your server supports pipelined HTTP messages. Even if it does, this can't be done in PHP, because properly implementing pipelined HTTP requests requires asyncronous I/O, in order to correctly handle protocol-level buffering issues, i.e. the socket's output buffer may be full, and cannot accept any more data, and so is the input buffer, because the server is waiting for you to fully read the previous requests's response, and drain the socket. Congratulations: you just deadlocked with the server! Commented Dec 31, 2015 at 0:32
  • I don't need the response, jsut Fire and Forget request (and the nodeJS server don't send it), but just to test : how can I empty socket's buffer? thx. Commented Dec 31, 2015 at 0:39
  • Have you tried fflush? stackoverflow.com/questions/2917584/… Commented Dec 31, 2015 at 0:42
  • fflush($this->socket); change nothing... Commented Dec 31, 2015 at 0:50
  • In my local Apache it works without needing to open socket every time. Could it be a matter of your PHP.ini config? Commented Dec 31, 2015 at 0:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.