francadaval
francadaval

Reputation: 2481

Firefox can't establish a websocket connection while Chrome can

I'm developing a webapp and I'm including websocket connectivity. I've installed a websocket server with node.js (5.0.0) with websocket (https://www.npmjs.com/package/websocket).

In Chrome it works perfectly but in Firefox this message appears in console:

Firefox no puede establecer una conexión con el servidor en wss://www.my-dev-server.com:1337/.

(Firefox can't establish a connection with server at...)

This is the server code (basically as in examples):

var WebSocketServer = require('websocket').server;

var https = require('https');
var fs = require('fs');

var options = {
  key: fs.readFileSync('/keyfile.key'),
  cert: fs.readFileSync('/pemfile.pem')
};
var port = 1337; 

// Create HTTPS service.
var server = https.createServer(options, function(request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(404);
    response.end();
});     

server.listen(port, function() {
    console.log((new Date()) + ' Server is listening on port ' + port);
});

// create the server
wsServer = new WebSocketServer({
    httpServer: server,
    autoAcceptConnections: false
});

function originIsAllowed( origin ) {
    // TODO: complete
    return true;
}

var clients = [];

// WebSocket server
wsServer.on('request', function(request) {

    if( !originIsAllowed( request.origin ) ) {
        request.reject();
        console.log((new Date()) + ' Connection from origin ' + request.origin + 'rejected.');
        return;
    }
    console.log((new Date()) + ' Connection accepted from ' + request.origin + '.');

    var connection = request.accept(null, request.origin);
    clients.push(connection);

    connection.on('message', function( message ) {
        if (message.type === 'utf8') {
            console.log('Received Message: ' + message.utf8Data);
        }
        else if( message.type === 'binary' ) {

        }
    });

    connection.on( 'error', function( error ) {
    });

    connection.on('close', function( reasonCode, description ) {
        console.log((new Date()) + " Peer " + connection.remoteAddress + " disconnected.");
    });
});

I use a self-signed certificate for development purposes, the same that is used by the web server.

This is my client code:

var connection = new WebSocket('wss://www.my-dev-server.com:1337');

connection.onopen = function () { };

connection.onerror = function (error) { };

connection.onmessage = function (message) {
    /* some code here */
};

Upvotes: 14

Views: 25316

Answers (2)

x-magix
x-magix

Reputation: 2852

this was weird but it works. I followed the answer from this link: https://support.mozilla.org/bm/questions/1350452

where user say's:

Tried: FF hamburger menu, Help, Troubleshoot mode... it started working in this no-extensions mode, and after restart to normal mode it is still working. So the workaround was to restart Firefox? :-/

So basically the answer is to restart your firefox. I guess error began with some badly adjusted about:config param or what so ever. Do not know

How to (it is said above just clearing it again):

Click on the Hamburger menu > Help > Troubleshoot mode... > Restart > Open than turn in off:Click on the Hamburger menu > Help > Turn troubleshoot mode off

Happy coding!

Upvotes: 0

francadaval
francadaval

Reputation: 2481

I, finally, found a solution.

The problem was that for https connections, in port 443, Firefox had already stored an exception for unknown certificate while it needed another exception for wss (port 1337 in this case).

I've added a certificate exception, in advanced preferences, for this port and now works fine.

Upvotes: 7

Related Questions