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

195
Visualizações
Adding information about the client on the initial Websocket handshake

I'm currently creating a distributed chat application. Everything works fine, meaning it's possible to send messages between the clients and the server and have it broadcasted appropiately.

However, at the moment only the actual message is sent to the server. I would like to add information about the user sending the message aswell.

I could add this information whenever I send a new message, but I would prefer if I could add this information during the initial handshake and then save this information on the backend.

I've thought about sending some information in the URL, but as I only instantiate the websocket once, this does not seem like the way to go. Similarly, I thought about adding the information as the body of the request, but I read that having a body on a GET request is usually not recommended.

So my question is, am I trying to do something that I should not be going for? Should I just send information about the client on each new message that is sent to the server?

Currently, my client looks like this:

const socket = new WebSocket("ws://localhost:8080/ws");

const connect = (cb) => {
    console.log("Attempting Connection...")

    socket.onopen = () => {
        console.log("Successfully Connected");
    }

    socket.onmessage = (msg) => {
        console.log(msg)
        cb(msg);
    }

    socket.onclose = (event) => {
        console.log("Socket Closed Connection: ", event);
    }

    socket.onerror = (error) => {
        console.log("Socket Error: ", error);
    }
};

const sendMsg = (msg) => {
    console.log("Sending msg: ", msg);
    socket.send(msg);
}

And the initial connection on the backend is handled by the following:

func serveWs(pool *websocket.Pool, w http.ResponseWriter, r *http.Request) {
    fmt.Println("WebSocket Endpoint Hit")

    conn, err := websocket.Upgrade(w, r)

    if err != nil {
        fmt.Fprintf(w, "%+V\n", err)
    }

    client := &websocket.Client{
        Name: "?????", // Obviously, I would like the actual name to be here.
        Conn: conn,
        Pool: pool,
    }

    pool.Register <- client

    client.Read()
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The normal thing to do here as selbie pointed out is to expect from the client a special first message through WebSocket. If that message is not recived or does not meet the requirements the server ends the WebSocket conn.

Using socket.onopen is very common for this task.

More on why you cant put headers: HTTP headers in Websockets client API

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