1
votes

so i switched to HTTPS, everything worked nicely exept the sockets.

If I try to visit website with the HTTP, the sockets connect, but if I try to connect with HTTPS I get:

The Error on Console:

Failed to load resource: net::ERR_CONNECTION_CLOSED
Failed to load resource: net::ERR_CONNECTION_CLOSED
Failed to load resource: net::ERR_CONNECTION_CLOSED
Failed to load resource: net::ERR_CONNECTION_CLOSED

Frontend:

function connect()
{
    if (!SOCKET)
    {
        var hash = getCookie('hash');
        if (hash == "") {
            //$.notify('You must login!', 'success');
        }
        if (hash != "") {
            $.notify('Connecting...', 'success');
        }
        SOCKET = io(':3001');
        SOCKET.on('connect', function(msg) {
            if (hash != "") {
                //$.notify('Connected!', 'success');
            }
            SOCKET.emit('hash', {
                hash: hash
            });
            $('#games tr').remove();
        });
        SOCKET.on('connect_error', function(msg) {
            $.notify('Connection lost!', 'success');
        });
        SOCKET.on('message', function(msg) {
            onMessage(msg);
        });

        SOCKET.on('disconnect', function(m) {
            SOCKET.emit('disconnect', {
                uhash: hash
            });
        });
    }
    else
    {
        console.log("Error: connection already exists.");
    }
}

Node.js/Backend

var httpsOptions = {
  cert: fs.readFileSync("/path/to/cert/cert.pem"),
  ca: fs.readFileSync("/path/to/cert/chain.pem"),
  key: fs.readFileSync("/path/to/cert/privkey.pem"),
}

var server = require('https').createServer(httpsOptions);
var io = require('socket.io').listen(server);
server.listen(3001);
1
What's the actual error? - Patrick Roberts
Socket is not connecting.. - Petras Vilkelis
AKA i get Connection lost! - Petras Vilkelis
I'm not an expert on HTTPS certificates, but would those actually be able to apply to port 3001? I don't think that would work. - Patrick Roberts
I imagine the port 3001 is internal, and accessed by Apache. I highly recommend using Apache for HTTPS instead of Node. - rubenwardy

1 Answers

0
votes

Your Node application should only accept HTTP connections. Your Apache server should be responsible for the HTTPS. This simplifies your app, and allows Apache to load balance for you if you ever decide to do so.

So between your app and Apache should be HTTP, and then HTTPS between Apache and the client. There are many guides on how to do this, I suggest reading the official documentation

If you decide to go against this, and implement SSL at the web app level (not recommended), then we'll need more information.