-->

Node js Faye Client not working properly with HTTP

2019-06-08 03:14发布

问题:

I tried to integrate node js with my application, I have just test the http server It works well, but when I use https server as following with my index.php to subscribe the message, This does not work.

Start a 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 a 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 Faye client to push message in 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 you,

Please tell me how to check is there any error on server side.

回答1:

I fixed issue my self, The issue was not in server side. It was in php Faye Client side. That Php Client works fine for HTTP server, but I need to use it for HTTPS server. I have done following changes then 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);
}