Edg*_*ter 4 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);
});
Run Code Online (Sandbox Code Playgroud)
现在,我进入邮递员中的“ https://127.0.0.1/a_service_name ”,接受证书后就得到了结果。当我在浏览器中转到该地址时,它还会给出结果,证书指示为“红色”,因为它是自签名的。
所以我的问题是以下。当我在浏览器中转到“ http://127.0.01 ”时,我没有得到“套接字”信息,而没有得到“ index.html”文件,只有空白页。我在控制台中收到以下错误
信息:(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>
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释如何获得警报消息吗?
编辑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");
Run Code Online (Sandbox Code Playgroud)
但是,feathers-socket.io的需求在app.js中,而不在index.js中。我想知道我是否可以移动那个?
正如达夫(Daffl)在这里的羽毛松弛通道 所指出的; 除了docs的https部分之外,在调用应用程序之前,请先查看明确需要in 的文档。将这两个放在一起,我会做这样的事情(未经测试):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);
Run Code Online (Sandbox Code Playgroud)
您的结构app.js
和index.js
完全取决于您。您可以如图所示在单个文件中完成上述所有操作,也可以将https / fs需求拆分为index.js,然后将应用程序配置为app.js-我建议您采用这种方法,因为它将允许您更改(如果您每个人都决定使用像nginx这样的反向代理来处理ssl而不是node,通常会使用index.js文件。
归档时间: |
|
查看次数: |
835 次 |
最近记录: |