Node.js套接字io连接错误ERR_CERT_AUTHORITY_INVALID

时间:2018-09-09 20:20:49

标签: node.js socket.io

我的错误是index.js:83 WebSocket connection to 'wss://54.38.211.175:3000/socket.io/?EIO=3&transport=websocket' failed: Error in connection establishment: net::ERR_CERT_AUTHORITY_INVALID

服务器代码

var fs = require('fs');
var options = {
  key: fs.readFileSync('privateKey.key'),
  cert: fs.readFileSync('certificate.crt')};
var app = require('https').createServer(options);
var io = require('socket.io')(app);
io.on('connection', function(socket) {
  socket.emit('on_test', {'x': 1});
});
app.listen(3000);

客户代码

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
<script>var HOST = "https://54.38.211.175:3000"; // PUT YOUR VPS IP THERE
    var SOCKET = null;
    if (!SOCKET) {
        SOCKET = io.connect(HOST,  {rejectUnauthorized: false ,secure: true,transports: ['websocket']});
        SOCKET["on"]("connect", function (b) {
          console["log"]("baglandim");

        });
        SOCKET["on"]("connect_error", function (a) {
          console.error("eror =" + a);
        });




      console.log(SOCKET);
    }</script>

我创建了ssl,但是它不起作用。我的网站可以使用SSL,因此我无法使用http连接到socketio

1 个答案:

答案 0 :(得分:1)

我解决了。 必须读取连接到socketio nodejs的网站的证书。

plesk

var fs = require("fs");

var options = {
  key: fs.readFileSync(
    "/usr/local/psa/var/modules/letsencrypt/etc/live/YOURSITE/privkey.pem"
  ),
  cert: fs.readFileSync(
    "/usr/local/psa/var/modules/letsencrypt/etc/live/YOURSITE/fullchain.pem"
  )
};
var app = require("https").createServer(options);
var io = require("socket.io").listen(app);
app.listen(2083);
//app.listen(PORT);


console.log('acigim');