feathersjs - > socketio https请求无效

时间:2017-09-26 05:20:28

标签: https socket.io feathersjs

我有一个在featherjs中的应用程序,我想用https运行。我已经开始工作了。我通过改变" index.js'来做到这一点。文件看起来像这样:

const fs = require('fs');
const https = require('https');
const app = require('./app');
const port = app.get('port');
const host = app.get('host');
//const server = app.listen(port);
const server = https.createServer({
    key: fs.readFileSync('./certs/aex007.key'),
    cert: fs.readFileSync('./certs/aex007.crt')
}, app).listen(port, function(){
    console.log("Mfp Backend started: https://" + host + ":" + port);
});

我现在就去,例如' https://127.0.0.1/a_service_name'在邮递员中,我在接受证书后得到了一个结果。当我在浏览器中访问该地址时,它也会给出结果,证书指示为“红色”。因为它是自我签名的。

所以我的问题如下。当我去' http://127.0.01'在浏览器中,而不是' index.html'文件我什么都没有得到我的' socket'信息,只有一个空白页面。我在控制台中收到以下错误

  

info:(404)路线:/socket.io/?EIO=3&transport=polling&t=LwydYAw-   页面未找到

然后' index.html'我正在使用的文件目前包含:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.3/socket.io.js"></script>
<script type="text/javascript" src="//cdn.rawgit.com/feathersjs/feathers-client/v1.1.0/dist/feathers.js"></script>
<script type="text/javascript">
    var socket = io('https://127.0.0.1:3001');
    var client = feathers()
        .configure(feathers.hooks())
        .configure(feathers.socketio(socket));
    var todoService = client.service('/some_service');

    todoService.on('created', function(todo) {
        alert('created');
        console.log('Someone created a todo', todo);
    });

</script>

有人可以向我解释如何获取警报消息吗?

编辑2017/09/27 我在互联网上发现socket.io配置为

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

var options = {
    key:    fs.readFileSync('ssl/server.key'),
    cert:   fs.readFileSync('ssl/server.crt'),
    ca:     fs.readFileSync('ssl/ca.crt')
};
var app = https.createServer(options);
io = require('socket.io').listen(app);     //socket.io server listens to https connections
app.listen(8895, "0.0.0.0");

然而,feat-socket.io的要求在app.js中而不是index.js。我想知道我是否可以移动它?

1 个答案:

答案 0 :(得分:6)

daffl feathers slack channel上指出here; check out the documentation除了https portion of the docs之外,还需要在feathers-socketio之前明确要求在应用上调用configure。把这两个放在一起,我会做这样的事情(未经测试):

const feathers = require('feathers');
const socketio = require('feathers-socketio');
const fs = require('fs');
const https = require('https');


const app = feathers();
app.configure(socketio());

const opts = {
  key: fs.readFileSync('privatekey.pem'),
  cert: fs.readFileSync('certificate.pem')
};

const server = https.createServer(opts, app).listen(443);

// magic sauce! Socket w/ ssl
app.setup(server);

app.jsindex.js的结构完全取决于您。您可以在一个文件中执行上述所有操作,或者将https / fs需求拆分为index.js,并将应用程序配置为app.js - 我建议使用此方法,因为它可以让您更改(通常较小的)index.js文件,如果你们每个人都决定使用像nginx这样的反向代理来处理ssl而不是节点。