Connection Management
Handle disconnects.
Connections Do Not Last Forever
Clients close tabs, lose Wi-Fi, or crash. Robust WebSocket servers must detect disconnects, clean up resources, and free goroutines. Otherwise you leak memory and sockets.
The Close Handshake
A clean shutdown sends a close frame in both directions. gorilla surfaces this as an error from ReadMessage with a close code you can inspect.
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
// graceful close
}All lessons in this course
- WebSocket Basics
- Using gorilla/websocket
- Broadcasting Messages
- Connection Management