Home  >  Article  >  Backend Development  >  How to properly close WebSocket connection in Golang

How to properly close WebSocket connection in Golang

PHPz
PHPzOriginal
2023-04-05 13:47:161533browse

WebSocket in Golang is a powerful network programming tool that provides us with a way to communicate in real time. When we use WebSocket to communicate, we may encounter situations where we need to close the WebSocket connection. So, how to correctly close the WebSocket connection in Golang? This article will introduce it to you in detail.

In Golang, we can use the net/http and github.com/gorilla/websocket libraries in the standard library to implement WebSocket connections. In both cases, there are methods for closing the WebSocket connection.

Close the WebSocket connection in the standard library

In the standard library, we can use the close method of WebSocket to close the connection. After receiving the close message, we can call the close method of WebSocket to close the connection. The code example is as follows:

func main() {
    http.HandleFunc("/ws", websocketHandler)
    http.ListenAndServe(":8080", nil)
}
func websocketHandler(w http.ResponseWriter, r *http.Request) {
    upgrader := websocket.Upgrader{
        ReadBufferSize:  1024,
        WriteBufferSize: 1024,
    }
    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Fatal("Upgrade: ", err)
    } 
    defer conn.Close() // 关闭 WebSocket 连接

    // 读写逻辑
}

In the above example, we created a WebSocket connection using the WebSocket package in the standard library, and called the conn.Close() method through the defer keyword after the read and write logic was completed. The connection was closed.

Close the WebSocket connection in the gorilla/websocket library

In the gorilla/websocket library, we can close the connection by calling the Close method of WebSocket. The code example is as follows:

func main() {
    http.HandleFunc("/ws", websocketHandler)
    http.ListenAndServe(":8080", nil)
}
func websocketHandler(w http.ResponseWriter, r *http.Request) {
    upgrader := websocket.Upgrader{
        ReadBufferSize:  1024,
        WriteBufferSize: 1024,
    }
    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Fatal("Upgrade: ", err)
    } 
    defer conn.Close() // 关闭 WebSocket 连接

    // 读写逻辑
    // 关闭 WebSocket 连接
    conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
}

In the above example, we used the gorilla/websocket library to create a WebSocket connection, and after the read and write logic ended, we sent a closing message to the other party by calling the conn.WriteMessage method. In this way, the other party can close the connection by receiving a close message.

Summary

Whether you are using the WebSocket package in the standard library or using the gorilla/websocket library, you can close the WebSocket connection through the corresponding method. In actual development, we need to choose the WebSocket library that suits us based on the actual situation in order to better realize the real-time communication function.

The above is the detailed content of How to properly close WebSocket connection in Golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn