With the popularity of Web applications, WebSocket, as one of the most commonly used communication protocols, is often used for real-time communication between clients and servers. In Go language, gorilla/websocket
is one of the most popular third-party libraries for using WebSocket. However, when using WebSocket, you often encounter the problem of frequent disconnection. Some tips for solving this problem, and related Go code examples are below.
1. Connection heartbeat
In the use of WebSocket, some factors may cause the connection to be disconnected, such as the loss of the browser/client network connection or the server-side suspended animation. To solve these problems, heartbeat operations can be used to maintain the connection. The heartbeat operation specifically sends a ping message to keep the WebSocket connection alive and starts a goroutine
to detect a response.
The following is a sample code:
package main import ( "log" "net/http" "time" "github.com/gorilla/websocket" ) var ( upgrader = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, CheckOrigin: func(r *http.Request) bool { return true }, } ) func main() { http.HandleFunc("/ws", wsHandler) log.Fatal(http.ListenAndServe(":8080", nil)) } func wsHandler(w http.ResponseWriter, r *http.Request) { conn, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Println("upgrade error:", err) return } defer conn.Close() heartbeat(conn) } func heartbeat(conn *websocket.Conn) { ticker := time.NewTicker(10 * time.Second) defer ticker.Stop() for { select { case <-ticker.C: if err := conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil { log.Println("write ping error:", err) return } } } }
In the above code, the heartbeat
function starts a goroutine
, which goroutine
Send ping messages every 10 seconds. When a ping message is sent, the select
statement waits 500 milliseconds to receive the pong message. If no pong message is received, the connection is disconnected or an error is returned. This example shows how to use heartbeats to maintain a WebSocket connection.
2. Reduce the frequency of client ping requests
In some cases, sending frequent ping requests may also interrupt the WebSocket connection. For example, when WebSocket is used with a corporate network proxy, the proxy may block continuous ping requests.
To avoid this situation, you can reduce the frequency of ping messages. For example, in the snippet below, Heartbeat sends ping requests at 10-second intervals. Controlling the ping frequency and adjusting it appropriately can improve the stability of WebSocket connections.
func heartbeat(conn *websocket.Conn) { ticker := time.NewTicker(10 * time.Second) defer ticker.Stop() for { select { case <-ticker.C: if err := conn.WriteMessage(websocket.PingMessage, []byte{}); err != nil { log.Println("write ping error:", err) return } } } }
3. Handle WebSocket closure
Regularly check whether the WebSocket connection is closed to avoid disconnection. You can add a defer
function to close the WebSocket connection to ensure that the WebSocket connection has been completely closed before the function exits.
func wsHandler(w http.ResponseWriter, r *http.Request) { conn, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Println("upgrade error:", err) return } defer conn.Close() ticker := time.NewTicker(10 * time.Second) defer ticker.Stop() for { _, _, err := conn.ReadMessage() if err != nil { log.Println("read error:", err) return } } }
In the above code, the error checking is collocated with the call to conn.ReadMessage()
. In a WebSocket connection, ReadMessage()
will block waiting for received messages, so we can use an infinite loop to regularly check whether the WebSocket has been closed to prevent entering Error check-call-error check -Infinity loop calling
.
Conclusion
The above tips can help you solve the problem of frequent disconnection of WebSocket connections. By utilizing the heartbeat mechanism, reducing ping frequency, and handling WebSocket shutdown, the stability of the WebSocket connection can be greatly improved, ensuring smoother real-time communication with the client.
The above is the detailed content of Tips to solve the frequent disconnection of Websocket connection in Go language. For more information, please follow other related articles on the PHP Chinese website!