我正在努力让2台服务器通过socket.io
库和SSL进行通信。
这曾经一直有效,直到升级socket.io
包(不能告诉你哪个)。
我已设法修复与浏览器的安全连接。我也让它在不安全的(http)服务器之间工作。但是安全(https)服务器拒绝在它们之间进行连接。你可能会说socket.io
不是为了服务器到服务器的通信而做的,但它可以为我节省大量的工作来解决它。
我现在正在跑步:
node: 7.5.0
express: 4.16.2
socket.io (and socket.io-client): 2.0.3
我甚至无法在下面编写简单的示例(删除所有中间件)。
节点服务器
// Use SSL certificate
const cert_path = "..";
const fs = require('fs');
const https_options = {
key: fs.readFileSync(cert_path+'/privkey.pem'),
cert: fs.readFileSync(cert_path+'/cert.pem')
};
const app = require('express')();
const https = require('https');
const server = https.createServer(https_options, app);
const io = require('socket.io')(server);
server.listen(8000);
io.on('connection', function (socket) {
console.log("connected");
});
节点客户端
const io = require('socket.io-client');
const socket = io.connect(
'https://localhost:8000',
{secure: true}
);
socket.on("connect", function () {
console.log("connected");
});
没有任何事情发生,他们都没有联系。知道为什么吗?
编辑:我在客户端每5秒就会出现connect_error
和reconnect_error
:
{ Error: xhr poll error
at XHR.Transport.onError (../node_modules/engine.io-client/lib/transport.js:64:13)
at Request.<anonymous> (../node_modules/engine.io-client/lib/transports/polling-xhr.js:128:10)
at Request.Emitter.emit (../node_modules/component-emitter/index.js:133:20)
at Request.onError (../node_modules/engine.io-client/lib/transports/polling-xhr.js:310:8)
at Timeout._onTimeout (../node_modules/engine.io-client/lib/transports/polling-xhr.js:257:18)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5) type: 'TransportError', description: 503 }
进一步挖掘错误,我发现它可能来自证书。但是当我应用SO的几个变通方法时,我会连续获得ECONNREFUSED,UNABLE_TO_VERIFY_LEAF_SIGNATURE,最后是DEPTH_ZERO_SELF_SIGNED_CERT ......
答案 0 :(得分:0)
努力之后:
connect
选项(安全,拒绝授权,...)process.env['NODE_TLS_REJECT_UNAUTHORIZED']
)我终于偶然发现了this page of github。它解决了我的问题,值得分享。
节点客户端
const https = require('https');
https.globalAgent.options.rejectUnauthorized = false;
const io = require('socket.io-client');
const sockets = io.connect('https://localhost:8001', {agent: https.globalAgent});
即使我希望首先获得授权我的连接,这对我也有用。