gobwas/ws-examples

how to send ping/pong hearbeat

Closed this issue · 4 comments

I send

conn.send(JSON.stringify({
  id:     1,
  method: "ping",
  params: "ping",
}));

and the header.OpCode is ws.OpText(0x1)

how can I make ping/pong heartbeat then?

Do you mean to send websocket ping/pong or your custom json-based ping/pong?

For websocket based ping/pong you could make this:

conn.Write(ws.CompiledPing)
...
conn.Write(ws.CompiledPong)

Note that CompiledPing is just result of:

ping := ws.Frame{
    Header: ws.Header{
        Fin: true, 
        OpCode: ws.OpPing,
    },
}
var buf bytes.Buffer
ws.WriteFrame(&buf, ping)
CompiledPing := buf.Bytes()

probably I am making a mistake. I realized that I cannot make ping/pong frame on js client side.
But actually AS you'd mentioned, should send ping/pong payload over websocket.
So I closed the issue. Thank you!

I'm attempting to send ping/pong frames both in GoLang. I'm confused if I should be attempting to use https://godoc.org/github.com/gobwas/ws/wsutil#ControlHandler. The examples doesn't demonstrate this scenario and I think it would be a great addition.

Using "Why use readData() for such case? Why not ReadMessage() or even ws.ReadHeader() and then handle frames manually?" Should make this quite easy.