【问题标题】:Node js Faye Client not working properly with HTTPSNode js Faye 客户端无法与 HTTPS 一起正常工作
【发布时间】:2015-05-28 09:17:38
【问题描述】:

我尝试将 node 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"));

谢谢,

请告诉我如何检查服务器端是否有任何错误。

【问题讨论】:

  • 它究竟是如何“不起作用”的?您是否收到错误,可能是意外输出?
  • 其实我期待 alert('Got a message:'); , 但我没有得到警报,也没有错误。

标签: php node.js ssl https faye


【解决方案1】:

我自己解决了问题,问题不在服务器端。它在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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 2016-01-31
    • 2017-09-23
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多