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

110
Visualizações
Authorize websocket connection with ws.js

I'm currently trying to write a websocket connector between the server and one exclusive client for real-time information exchange between the two using ws.js. I would like that the server automatically asks the client for authentication what i have looks like this:

const ws = require("ws");
const pass = "1234"

class WebsocketHandler {
    constructor(port = 8080) {
        this.server = new ws.Server();
        this.server.on("connection", this.startHandleConnection.bind(this));
    }
    startHandleConnection(wsc) {
        this.wsc = wsc;
        //Authorize the client with a password before accepting the connection
        wsc.on("message", this.handleMessage.bind(this));
        wsc.send("Authorize"); // The client-side is expected to respond with the password
    }
    handleMessage(message) {
        if(!this.authorized && message !== pass) {
           delete this.wsc;
        } else {
           //do stuff
        }
    }
}

Is there a more elegant approach to this?

about 4 years ago · Juan Pablo Isaza
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