我尝试将节点js与我的应用程序集成,我刚刚测试了http服务器它运行良好,但是当我使用https服务器跟随我的index.php来订阅消息时,这不起作用。
启动服务器
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);
创建客户端
<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>
发送讯息
我使用Faye客户端在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"));
谢谢,
请告诉我如何检查服务器端是否有任何错误。
答案 0 :(得分:4)
我修复了自己的问题,问题不在服务器端。它在php Faye Client方面。该Php客户端适用于 HTTP服务器,但我需要将其用于 HTTPS服务器。我做了以下更改,然后工作正常。
<强> /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);
}