Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

304
Visualizações
Chrome WebSocket connection closes immediately

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?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda