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

541
Visualizações
Websocket: Is it necessary to send ping messages by hand? (e.g between fiber websocket server & js client in browser)

I've wrtten a websocket server in fiber's websocket, and the client is js websocket in browser.

Some questions regarding ping/pong:

  1. Is there any default ping/pong msg between the websocket server & client?
    If yes, then why I can't see it in chrome's devtools.
  2. If there isn't any defult ping/pong msg, should I add ping/pong between server & client myself?
  3. If it's a good practice to add ping/pong msg myself, then:
  4. Is it better to send ping from client to server, or reverse?
  5. Since conn.ReadJSON() will block, does that means I should use conn.SetPingHandler() to specify a handler, or just use the default handler?
    In either case, the ping need to be sent from client to server I guess.

Sample ping from server (not sure is it proper)

I added a testing ping sent from fiber websocket server to js client, if needed:


        // ping
        // TODO: not sure is this needed ?
        // TODO: use goroutine pool ?
        if needPing {
            go func() {
                pingTicker := time.NewTicker(pingPeriod)
                defer func() {
                    pingTicker.Stop()
                }()
                time.Sleep(pingPeriod) // wait a while before start ping,
                for {
                    select {
                    case <-pingTicker.C:
                        conn.WriteControl(websocket.PingMessage, []byte("ping"), time.Now().Add(time.Second*5))
                        log.Debug("ping")
                    }
                }
            }()
        }

I also don't see any ping msg on client in chrome's devtools.

It's better to use for{ select } to handle both ping & normal msg I guess, but conn.ReadJSON() would block the goroutine, might need to create a channel for it? Didn't try yet.

BTW, any suggestion to the ping impl?

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