Node.js Socket.io HTTPS - 传输轮询

时间:2017-11-30 10:17:37

标签: node.js ssl nginx https socket.io

我正在使用nginx和我的ssl证书,并且它给了我网站的https版本,这很棒。问题是我的socket.io通信不起作用,除非我使用常规的http连接。如果这有点长,我很抱歉,但我不确定我在这里做什么,并想确保你们有你可能需要知道的一切。我尝试了各种不同网站的解决方案,其中一些在这里,但没有一个工作。

我尝试手动创建https服务器,而不是让快递执行,但这导致根本没有加载,使用当前的实现,我至少可以看到该网站。

upstream project {
   server example.org:4000;
}

server {
   listen 80;
   return https://$host$request_uri;
}

server {
   listen 443 ssl default_server;
   listen [::]:443 ssl default_server;
   ssl_certificate /home/adam/SSL/public.crt;
   ssl_certificate_key /home/adam/SSL/example.org.key;
   ssl_prefer_server_ciphers on;

   location / {
     proxy_pass http://example.org;
   }
}

这似乎正常,当我访问我的网站时,它会自动转到https版本并加载页面。问题是客户端何时尝试连接我不断获取:

" https://MY_SERVER_IP:4040/socket.io/?EIO=3&transport=polling&t=M0CPjUDnet::ERR_CONNECTION_CLOSED"

打印到控制台

这是我的客户端和服务器代码:

var IPaddress = 'https://MY_SERVER_IP:4040';
var socket = io.connect(IPaddress,{secure:true});
socket.on('connect', function (socket) {
    console.log('Connected!');
});

服务器代码:

var express = require('express');
var app = express();
var privateKey = fs.readFileSync(__dirname+'/SSL/example.com.key','utf8');
var certificate = fs.readFileSync(__dirname+'/SSL/public.crt','utf8');
var intermediate = fs.readFileSync(__dirname+'/SSL/intermediate.crt','utf8');
var options = {key:privateKey,cert:certificate,ca:intermediate};

var io = require('socket.io').listen(4040,options);
//var io = require('socket.io').listen(4040);
io.sockets.on('connection', function (socket) {
    socket.on('disconnect',function(){
       console.log("A client has left us :(");
    });
});
app.listen(4000);

更新 - 02/12/2017

在我的代码中,我有这一行:

require('./routes.js')(app);

包含:

module.exports = function(app) {

    app.get('/main',function(req,res){
        if (req.session.user == null){
            // if user is not logged-in redirect back to login page //
            res.redirect('/');
        }   else{
            res.sendFile(path.join(__dirname + '/FrontEnd/main.html'));
        }
    });

    // viewed at http://localhost:8080
    app.get('/', function(req, res) {
        if(req.cookies.user == undefined || req.cookies.pass == undefined){
            res.sendFile(path.join(__dirname + '/FrontEnd/login.html'));
        }else {
            //attempt automatic login
            AM.autoLogin(req.cookies.user,req.cookies.pass,function(o){
               if(o !=null){
                   req.session.user = o;
                   res.sendFile(path.join(__dirname + '/FrontEnd/home.html'));
               }else{
                   res.sendFile(path.join(__dirname + '/FrontEnd/login.html'));
               }
            });

        }
    });

...

这会导致502错误的网关错误吗?

1 个答案:

答案 0 :(得分:0)

扩展我的评论,在当前的服务器代码中,express和socket.io只接受常规的HTTP连接。 socket.io listen(port[, options])函数不接受HTTPS连接。

您通过手动创建HTTPS服务器开始了正确的方法。然后,您需要将express和socket.io附加到该服务器。

var fs = require('fs');
var express = require('express');
var app = express();
var privateKey = fs.readFileSync(__dirname + '/SSL/example.com.key', 'utf8');
var certificate = fs.readFileSync(__dirname + '/SSL/public.crt', 'utf8');
var intermediate = fs.readFileSync(__dirname+'/SSL/intermediate.crt', 'utf8');
var options = { key: privateKey, cert: certificate, ca: intermediate };

// Create our HTTPS server here and attach express to it
var server = require('https').createServer(options, app);

// Attach socket.io to our existing HTTPS server
var io = require('socket.io')(server); 

io.sockets.on('connection', function (socket) {
    socket.on('disconnect', function() {
       console.log("A client has left us :(");
    });
});
server.listen(4000);