使用 socket.io 和 HTTPS 而不是 HTTP
Use socket.io with HTTPS instead of HTTP
我正在使用自签名证书来加密流量数据。
我的 .crt 和 .key 文件位于 /etc/nginx/ssl/
(some_file.key 和 some_file.crt)
我在 http 上使用 socket.io,但试图将它转到 https。这是我的实际代码:
var formidable = require('formidable');
var express = require('express');
var fs = require('fs');
var privateKey = fs.readFileSync('../etc/nginx/ssl/some_file.key').toString();
var certificate = fs.readFileSync('../etc/nginx/ssl/some_file.crt').toString();
//how can I exclude this? (I have no intermediate, should I?)
var ca = fs.readFileSync('../intermediate.crt').toString();
var app = express.createServer({key:privateKey,cert:certificate,ca:ca });
var io = require('socket.io');
app.listen(3000, function(){
//wait on 3000
});
app.post('/posts', function(req, res){
//server communication
});
io.on('connection', function(socket){
//wait on connections
});
客户端:
var socket = io(url + ":3000", { "secure": true, 'connect timeout': 5000 });
这是正确的做法吗?我的 https 代码基于示例,所以我怀疑这是否足够好(我知道这不够好,但应该接近)。当我 运行 代码时,我也得到一个错误 no such file or directory '../etc/nginx/ssl/some_file.key'
...
我是这样使用的,尽管这在很大程度上取决于您使用的 express 版本。这是版本 3.4
var express = require('express')
, app = express()
,fs = require('fs')
,events = require('events');
...
var options = {
key: fs.readFileSync('/etc/nginx/ssl/some_file.key'),
cert: fs.readFileSync('/etc/nginx/ssl/some_file.crt')
};
/*
*Configuration
*
*/
var server = require('https').createServer(options, app), io = require("socket.io").listen(server);
var port = 8443;
var ipaddr = '0.0.0.0';
app.configure(function() {
app.set('port', port);
app.set('ipaddr', ipaddr);
app.use(express.bodyParser());
app.use(express.methodOverride());
...
});
我正在使用自签名证书来加密流量数据。 我的 .crt 和 .key 文件位于 /etc/nginx/ssl/
(some_file.key 和 some_file.crt)
我在 http 上使用 socket.io,但试图将它转到 https。这是我的实际代码:
var formidable = require('formidable');
var express = require('express');
var fs = require('fs');
var privateKey = fs.readFileSync('../etc/nginx/ssl/some_file.key').toString();
var certificate = fs.readFileSync('../etc/nginx/ssl/some_file.crt').toString();
//how can I exclude this? (I have no intermediate, should I?)
var ca = fs.readFileSync('../intermediate.crt').toString();
var app = express.createServer({key:privateKey,cert:certificate,ca:ca });
var io = require('socket.io');
app.listen(3000, function(){
//wait on 3000
});
app.post('/posts', function(req, res){
//server communication
});
io.on('connection', function(socket){
//wait on connections
});
客户端:
var socket = io(url + ":3000", { "secure": true, 'connect timeout': 5000 });
这是正确的做法吗?我的 https 代码基于示例,所以我怀疑这是否足够好(我知道这不够好,但应该接近)。当我 运行 代码时,我也得到一个错误 no such file or directory '../etc/nginx/ssl/some_file.key'
...
我是这样使用的,尽管这在很大程度上取决于您使用的 express 版本。这是版本 3.4
var express = require('express')
, app = express()
,fs = require('fs')
,events = require('events');
...
var options = {
key: fs.readFileSync('/etc/nginx/ssl/some_file.key'),
cert: fs.readFileSync('/etc/nginx/ssl/some_file.crt')
};
/*
*Configuration
*
*/
var server = require('https').createServer(options, app), io = require("socket.io").listen(server);
var port = 8443;
var ipaddr = '0.0.0.0';
app.configure(function() {
app.set('port', port);
app.set('ipaddr', ipaddr);
app.use(express.bodyParser());
app.use(express.methodOverride());
...
});