Saturday, 30 March 2019

Can't connect to websocket using ssl and apache

I am tying to connect my client to the server socket using socket.io. When I am using http all works fine but when I try to use https the client can't connect.

I try to create the server using http require('https') and using certificates but didn't work.

For now after a few code changes and tests this is how my code is:

Server, index.js

var https = require('https');
var app = express();
var options = {
  key: fs.readFileSync('./file.pem'),
  cert: fs.readFileSync('./file.crt')
};


var server = https.createServer(options, app);
var io = require('socket.io')(server);


server.listen(3003, function() {
        console.log('server up and running at %s port', 3003);
});
server.on('connection', function(client){
        console.log("NUEVO CLIENTE");
        client.on('event', function(data){});
        client.on('disconnect', function(){});
        client.on('room', function(room) {
                console.log('Room: '+room);
                        client.join(room);
    });
        client.on('leaveRroom', function(room) {
                        console.log('LeaveRroom: '+room);
                        client.leave(room);
    });

});

The server connection always success using port 3003.

Client

$scope.socket = io.connect('https://localhost:3003/', { transports: ['websocket'] });
$scope.socket.on('connect_error', function (data) {
                console.log(data);
    });

    $scope.socket.on('message', function(message) {
        $scope.getAttendance();
        $scope.clientDetails(message.user[0]);
    })


Browser logs:

VM2481:7 WebSocket connection to 'wss://localhost:3003/socket.io/?EIO=3&transport=websocket' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
r.doOpen @ VM2481:7
r.open @ VM2481:7
r.open @ VM2481:6
r @ VM2481:6
r @ VM2481:6
r.open.r.connect @ VM2481:6
(anonymous) @ VM2481:6
VM2481:6 engine.io-client:socket socket error {"type":"TransportError","description":{"isTrusted":true}} +93ms
VM2481:6 socket.io-client:manager connect_error +92ms
VM2481:6 socket.io-client:manager cleanup +4ms
access.js:45 Error: websocket error
    at r.onError (eval at <anonymous> (jquery.min.js:2), <anonymous>:7:8015)
    at WebSocket.ws.onerror (eval at <anonymous> (jquery.min.js:2), <anonymous>:7:23668)
VM2481:6 socket.io-client:manager reconnect attempt error +5ms
VM2481:6 socket.io-client:manager will wait 5000ms before reconnect attempt +2ms
VM2481:6 engine.io-client:socket socket close with reason: "transport error" +16ms
VM2481:6 socket.io-client:manager attempting reconnect +5s
VM2481:6 socket.io-client:manager readyState closed +3ms
VM2481:6 socket.io-client:manager opening https://localhost:3003/ +3ms
VM2481:6 engine.io-client:socket creating transport "websocket" +5s
VM2481:6 engine.io-client:socket setting transport websocket +3ms
VM2481:6 

For the ssl I am using load balancer for AWS.

I expect the client connect successfully with the server over https.



from Can't connect to websocket using ssl and apache

No comments:

Post a Comment