[英]Socket.IO can't connect through https
I have a node.js app, that uses socket.IO. 我有一个node.js应用程序,它使用socket.IO。 It works fine on http, but when trying to connect to the socket through https - nothing happens.
它在http上工作正常,但在尝试通过https连接到套接字时 - 没有任何反应。
Here's some part of the code: 这是代码的一部分:
var fs = require('fs');
var ioHttp = require('socket.io').listen(8899, {
'flash policy port': -1
});
initSocket(ioHttp);
var ioHttps = require('socket.io').listen(8895, {
key: fs.readFileSync('/path/to/file/file.key'),
cert: fs.readFileSync('/path/to/file/file.crt'),
ca: [
fs.readFileSync('/path/to/file/sub.class1.server.ca.pem'),
fs.readFileSync('/path/to/file/ca.pem')
],
'flash policy port': -1
});
initSocket(ioHttps);
and the initSocket
function: 和
initSocket
函数:
function initSocket(io) {
io.enable('browser client minification');
io.enable('browser client etag');
io.enable('browser client gzip');
io.set('transports', [
'websocket',
'htmlfile',
'flashsocket',
'jsonp-polling'
]);
io.sockets.on('connection', function (client) {
//the connnection is handled here
});
}
The client connect like this: 客户端连接如下:
var secureConnection = false;
var port = 8899;
if (window.location.protocol === 'https:') {
port = 8895;
secureConnection = true;
}
var socket = io.connect('domain.org', {port: port, secure: secureConnection});
As I said everything works fine on http, but connecting on https gives me "The connection was interrupted". 正如我所说的一切在http上工作正常,但在https上的连接给了我“连接被中断”。 What am I doing wrong?
我究竟做错了什么?
You cannot initalize socket.io
server like https
server. 你不能像
https
服务器那样初始化socket.io
服务器。 You have to start a separate https server and then attach socket.io server to it. 您必须启动一个单独的https服务器,然后将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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.