-
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
[question] Emit ping via WriteMessage() or WriteControl()? #652
Comments
Assuming both are ok, is
https://pkg.go.dev/github.com/gorilla/websocket#hdr-Concurrency |
WriteControl can be called concurrently with other write methods. WriteMessage cannot. Control messages (ping is a control message) can be written with both methods. In the chat example, all application writes to the connection are from a single goroutine. Because concurrency is not an issue here, either method can be used. The command example writes ping messages from a different goroutine than the goroutine that writes data messages. WriteControl or some form of synchronization is required in the example. WriteControl can write a message concurrently with WriteMessage because control messages can be interleaved with a data message's frames in the websocket protocol. There's no downside to calling WriteControl for all control messages. |
@TaylorRex ok, thanks for the confirmation! So I should be able to use WriteControl() if I can't structure my app to ping from the same goroutine. But I give up the ability to use things like SetWriteDeadline() for the ping message itself, like in the chat example. |
Use the WriteControl deadline argument to specify a deadline for writes using the method. |
Oh, perfect! |
What exactly does this mean? |
Are both idiomatic?
What are the trade-offs?
Chat example emits ping using
WriteMessage()
websocket/examples/chat/client.go
Line 116 in 58729a2
Command example emits ping using
WriteControl()
websocket/examples/command/main.go
Line 87 in 5ed622c
The text was updated successfully, but these errors were encountered: