-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Websocket ping pong timeout #866
Comments
Control messages like PING are processed when reading a message. Ensure that the client reads all messages.
NextReader and NextWriter are the core functionality ReadMessage is a helper method for getting a reader using NextReader and reading from that reader to a buffer. WriteMessage is a helper method for getting a writer using NextWriter, writing the message and closing the writer.
Because your application does not write control messages concurrently with calls to WriteMessage / NextWriter, there's no need to use WriteControl in your application. Applications should write with a deadline to protect against peers that do not read from the socket. |
Hi @pennystevens, Thanks for getting back to me to provide clarity on this.
I'm running the app in a
func getWSConn(u *userdom.User, roomID string) (*websocket.Conn, error) {
conn, _, err := websocket.DefaultDialer.Dial("wss://localhost:8090/ws...", nil)
conn.SetPingHandler(func(data string) error {
println("RECEIVED PING") // OK
return conn.WriteMessage(websocket.PongMessage, []byte{})
})
return conn, err
} ... and the creation of the PONG handler in the app: func createWS(w http.ResponseWriter, r *http.Request, pongHandler pongHandler) (*websocket.Conn, error) {
// ...
conn.SetPongHandler(pongHandler)
return conn, nil
}
// ...
// Respond to ping tick and reset the timer
func (c *WSClient) pongHandler(pongMsg string) error {
log.Print("PONG") // NOT REACHED
return c.wsconn.SetReadDeadline(time.Now().UTC().Add(pongWait))
} Is there something I'm missing? |
I noticed some problems with the code in issue. This code:
will never report a ping or pong message because ReadMessage only returns data messages (text, binary). Control messages are handled by callbacks or, in the case of close, the error return value. The ping handler:
has three problems.
Fix the ping handler by starting from the default ping handler code:
The previous comment sets the pong handler to |
Hi guys,
I'm trying to build a chat app that looks like the below simplified version.
What the chat app is doing:
What the stress-test script is doing
Problem
The stress-test script receives a PING frame and tries to send a PONG one but I see no incoming PONG frame in the chat app and the connection is then timed out.
wsconn.ReadMessage() error: read tcp 127.0.0.1:8090->127.0.0.1:55896: i/o timeout
Subsidiary questions
ReadMessage/WriteMessage
vsNextReader/NextWriter
?WriteMessage
to send control frames. I saw that there's alsoWriteControl
that takes a deadline and then callsSetWriteDeadline
. I know that unlikeWriteMessage
,WriteControl
is safe to be used concurrently. The thing is my chat app, I only set a read deadline cause the "idleness" of a connection is detected when the user leaves, so from a server-side perspective, when there's no more data to be read. I don't know how am I supposed to useWriteControl
in this context.error:websocket: close 1006 (abnormal closure): unexpected EOF
.What can cause this issue cause I don't have much log?
Originally posted by @BigBoulard in gorilla/.github#26
The text was updated successfully, but these errors were encountered: