skip to Main Content

I have been trying to setup a wss server using nodejs, and have encountered a problem when trying to connect to it using chrome. The problem still occurs with all extensions disabled and in an incognito window so I’ve ruled that out as the problem.

When trying to connect using chrome, I get the error:

WebSocket connection to 'wss://www.domain-name.com/' failed:

with no reason given. On the server, socket.on(‘close’) is called immediately with description "Connection dropped by remote peer" The close event has wasClean = false. This error does not occur when connecting from safari and Firefox so I’m not really sure where to look to see what’s causing it. It’s running on AWS Lightsail, and through an Apache proxy server.

The client code:

var socket = new WebSocket("wss://www.domain-name.com", 'JSON')
socket.onopen = function (event) {
    console.log('open');
    socket.send('socket opened')};

socket.onclose = function (event) {
    console.log(event)};

socket.onmessage = function(message) {
    console.log('receiving message from server...')};

And the server code:

const WebSocketServer = require('websocket').server;
app = express()
var server = app.listen(3000, () => {
    console.log('Server started');
});

app.use(express.static('public'));

var wsServer = new WebSocketServer({
    httpServer: server
});

wsServer.on('request', function(request){
    console.log('New connection');
    var connection = request.accept(null, request.origin);

    connection.send('welcome from server...');

    connection.on('message', function(message){
        console.log(message)};

    connection.on('close', function(reasonCode, description) {
        console.log('disconnecting', reasonCode, description);
        });
});

I also got the same error before switching to a secure WebSocket server. Any help would be appreciated, I’ve run out of places to look and ways to try and get more information to help out what the problem is.

EDIT: it seems to work on chrome on my phone, but not on chrome on my friends phone?

2

Answers


  1. Chosen as BEST ANSWER

    The problem was not specifying the protocol when accepting the connection. After about 20 hours working on the same bug and implementing an SSL certificate to get it to work, I changed:

    request.accept(null, request.origin);
    

    to:

    request.accept('json', request.origin);
    

    For some reason the chrome gives a really unhelpful error message. Microsoft edge the same error occurs, but gives a much more helpful error message so I could work out what was going on.


  2. In my case, this was caused by passing an unused options value as the third parameter to the WebSocket constructor. The options parameter is supported by Node.js’s ws module but not by browsers; however, instead of displaying a clean error message, Chrome closed the connection without a good description.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search