Node js Faye Client not working properly with HTTPS

I tried to integrate node js with my application, I just checked out the HTTP server. It works well, but when I use an https server following my index.php to subscribe to a post, it doesn't work.

Start the server

var https = require('https'),
    faye = require('faye');
var fs = require('fs');

var options = {
  key: fs.readFileSync('/etc/apache2/ssl/apache.key'),
  cert: fs.readFileSync('/etc/apache2/ssl/apache.crt')
};

var server = https.createServer(options),
     bayeux = new faye.NodeAdapter({mount: '/'});

bayeux.attach(server);
server.listen(1337);

      

Create client

<script src="faye-browser-min.js"></script>
<script>
var client = new Faye.Client('https://localhost:1337/');

client.subscribe('/messages/*', function(message) {
  alert('Got a message:');
});
</script>

      

Send messages

I used the Faye client to inject a post into test.php.

 $adapter = new \Nc\FayeClient\Adapter\CurlAdapter();
 $client = new \Nc\FayeClient\Client($adapter, 'https://localhost:1337/');

 $client->send("/messages/test", array("name" => "foo"), array("token" => "456454sdqd"));

      

Thank,

Please tell me how to check if there is a server side error.

+1


source to share


1 answer


I fixed the problem myself, the problem was not server side. It was in php Faye Client . This php client works fine for an HTTP server , but I need to use it for an HTTPS server . I made the following changes, but it works fine.

/vendor/nc/faye-client/src/Nc/FayeClient/Adapter/CurlAdapter.php



public function postJSON($url, $body)
{

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($body),
            ));

    curl_exec($curl);
    curl_close($curl);
}

      

+4


source







All Articles