简体   繁体   中英

Node.js Socket.io HTTPS - transport polling

I am using nginx with my ssl certificates and it's giving me the https version of my site which is great. The problem is my socket.io communications don't work unless i use a regular http connection. I apologize if this is a bit long, but i'm not sure what i'm doing here and wanted to make sure you guys had everything you might need to know. I have tried the solutions of various different sites some of which were on here, but none of them worked.

I tried manually creating the https server instead of letting express do it but that resulted in nothing loading at all, with the current implementation i can at least see the site.

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;
   }
}

That seems to be working as when i go to my site, it automatically takes me to the https version and the page is loaded. The issue is when is when the client side tries to connect i keep getting:

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

printed to the console

Here's my client and server code:

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

server code:

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);

Update - 02/12/2017

In my code i have this line:

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

which contains:

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'));
               }
            });

        }
    });

......

Could this be causing the 502 bad gateway error?

Expanding on my comment, in your current server code, both express and socket.io are only accepting regular HTTP connections. The socket.io listen(port[, options]) function does not accept HTTPS connections.

You started on the right approach with manually creating an HTTPS server. You then need to attach express and socket.io to that server.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM