Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 */
};
like image 936
francadaval Avatar asked Nov 08 '15 07:11

francadaval


People also ask

Why Firefox can't establish a connection to the server?

The site could be temporarily unavailable or too busy. Try again in a few moments. If you are unable to load any pages, check your computer's network connection. If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web.

Does Firefox support WebSockets?

WebSocket, as an IETF standard, and with a W3C browser API, is fully supported by all modern browsers: Chrome 16 + (incl. Chrome for Android) Firefox 11 + (incl.

How do I fix WebSocket connection error?

Check that all the Bot Insight services are running. Check that your firewall settings are configured to accept incoming websocket data. Try to use a different web browser. Restart the Bot Insight Visualization and Bot Insight Scheduler services.


1 Answers

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.

like image 72
francadaval Avatar answered Oct 25 '22 23:10

francadaval