节点js Faye客户端无法正常使用HTTPS [英] Node js Faye Client not working properly with HTTPS

查看:176
本文介绍了节点js Faye客户端无法正常使用HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将节点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.listen(1337);




创建客户




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

client.subscribe('/ messages / *',function(message){
alert('有消息:');
});
< / 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));

谢谢,



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

解决方案

我修复了自己的问题,问题不在服务器端。它位于 php Faye Client 一侧。该Php客户端适用于 HTTP服务器,但我需要将其用于 HTTPS服务器。我做了以下更改然后它工作正常。



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

 公共职能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,数组(
'Content-Type:application / json',
'Content-Length:'。strlen($ body),
)) ;

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


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.

解决方案

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);
}

这篇关于节点js Faye客户端无法正常使用HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆