Socket.IO can't connect through https

前端 未结 1 1827
日久生厌
日久生厌 2020-12-07 21:15

I have a node.js app, that uses socket.IO. It works fine on http, but when trying to connect to the socket through https - nothing happens.
Here\'s some part of the code

相关标签:
1条回答
  • 2020-12-07 22:05

    You cannot initalize socket.io server like https server. You have to start a separate https server and then attach socket.io server to it.

    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");
    
    0 讨论(0)
提交回复
热议问题