Home > OS >  How do I make a pong response from golang.org/x/websocket?
How do I make a pong response from golang.org/x/websocket?

Time:10-14

I would like to be able to respond "pong" when I get "ping" for the keep alive connection on golang.org/x/websocket impl.

It seems that it's not supported natively. Is it possible to implement it? How to proceed?

srv := &http.Server{
        Addr:              ":3000",
        ReadTimeout:       12 * time.Second,
        IdleTimeout:       12 * time.Second,
        WriteTimeout:      12 * time.Second,
        ReadHeaderTimeout: 12 * time.Second,
    }

    http.Handle("/websocket", websocket.Handler(handler))

CodePudding user response:

The golang.org/x/net/websocket package automatically responds to a PING message with a PONG message. Application code is not required to enable the feature.

View the code here.

  • Related