【发布时间】:2017-12-20 16:58:01
【问题描述】:
所以我切换到 HTTPS,除了套接字之外,一切都运行良好。
如果我尝试使用 HTTP 访问网站,则套接字会连接,但如果我尝试使用 HTTPS 连接,则会得到:
控制台错误:
Failed to load resource: net::ERR_CONNECTION_CLOSED
Failed to load resource: net::ERR_CONNECTION_CLOSED
Failed to load resource: net::ERR_CONNECTION_CLOSED
Failed to load resource: net::ERR_CONNECTION_CLOSED
前端:
function connect()
{
if (!SOCKET)
{
var hash = getCookie('hash');
if (hash == "") {
//$.notify('You must login!', 'success');
}
if (hash != "") {
$.notify('Connecting...', 'success');
}
SOCKET = io(':3001');
SOCKET.on('connect', function(msg) {
if (hash != "") {
//$.notify('Connected!', 'success');
}
SOCKET.emit('hash', {
hash: hash
});
$('#games tr').remove();
});
SOCKET.on('connect_error', function(msg) {
$.notify('Connection lost!', 'success');
});
SOCKET.on('message', function(msg) {
onMessage(msg);
});
SOCKET.on('disconnect', function(m) {
SOCKET.emit('disconnect', {
uhash: hash
});
});
}
else
{
console.log("Error: connection already exists.");
}
}
Node.js/后端
var httpsOptions = {
cert: fs.readFileSync("/path/to/cert/cert.pem"),
ca: fs.readFileSync("/path/to/cert/chain.pem"),
key: fs.readFileSync("/path/to/cert/privkey.pem"),
}
var server = require('https').createServer(httpsOptions);
var io = require('socket.io').listen(server);
server.listen(3001);
【问题讨论】:
-
实际错误是什么?
-
套接字未连接..
-
又名我失去了连接!
-
我不是 HTTPS 证书方面的专家,但那些真的能够申请到 3001 端口吗?我认为这行不通。
-
我想端口 3001 是内部的,由 Apache 访问。我强烈建议将 Apache 用于 HTTPS 而不是 Node。
标签: html node.js websocket socket.io