英文:
correct closing of the websocket connection by the server
问题
TryCloseNormally
函数用于从服务器端关闭WebSocket连接。有两个版本的这个函数。第一个版本只发送一个关闭请求。而第二个版本则等待客户端的响应。哪个版本的函数是正确的?
版本1:
func TryCloseNormally(wsConn *websocket.Conn) error {
closeNormalClosure := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")
if err := wsConn.WriteControl(websocket.CloseMessage, closeNormalClosure, time.Now().Add(time.Second)); err != nil {
return err
}
return wsConn.Close()
}
版本2:
// 根据RFC 6455标准,该函数尝试关闭连接。
// 该函数总是关闭连接。
//
// 为了处理在发送关闭消息之前对等方发送数据消息的情况,
// 函数读取并丢弃数据消息,直到返回Normalclose。
func TryCloseNormally(wsConn *websocket.Conn, closeCode int, textErr string) error {
defer wsConn.Close()
closeNormalClosure := websocket.FormatCloseMessage(closeCode, textErr)
if err := wsConn.WriteControl(websocket.CloseMessage, closeNormalClosure, time.Now().Add(time.Second)); err != nil {
return err
}
if err := wsConn.SetReadDeadline(time.Now().Add(time.Second)); err != nil {
return err
}
for {
_, _, err := wsConn.ReadMessage()
if websocket.IsCloseError(err, closeCode) {
return nil
}
if err != nil {
return err
}
}
}
我需要等待客户端的响应吗?或者它不会影响任何事情。
英文:
The TryCloseNormally
function closes the websocket connection from the server side. There are two versions of this function. The first one just sends a closing request. And the second one is waiting for a response-a copy from the client. Which of these functions is correct?
version 1
func TryCloseNormally(wsConn *websocket.Conn) error {
closeNormalClosure := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")
if err := wsConn.WriteControl(websocket.CloseMessage, closeNormalClosure, time.Now().Add(time.Second)); err != nil {
return err
}
return wsConn.Close()
}
version 2
// The function tries to close the connection according to the RFC 6455 standard.
// The function ALWAYS closes the connection.
//
// To handle the case where the peer sent a data message before the sending the close message,
// function read and discard data messages until an Normalclose is returned.
func TryCloseNormally(wsConn *websocket.Conn, closeCode int, textErr string) error {
defer wsConn.Close()
closeNormalClosure := websocket.FormatCloseMessage(closeCode, textErr)
if err := wsConn.WriteControl(websocket.CloseMessage, closeNormalClosure, time.Now().Add(time.Second)); err != nil {
return err
}
if err := wsConn.SetReadDeadline(time.Now().Add(time.Second)); err != nil {
return err
}
for {
_, _, err := wsConn.ReadMessage()
if websocket.IsCloseError(err, closeCode) {
return nil
}
if err != nil {
return err
}
}
}
Do I need to wait for a response from the client? Or it doesn't affect anything.
答案1
得分: 1
两种都是正确的。
第二个函数实现了关闭握手。握手使得服务器能够读取客户端发送的所有消息。
如果可以丢弃客户端发送的消息,可以使用第一个函数。
英文:
Both are correct.
The second function implements the closing handshake. The handshake makes it possible for the server to read all messages sent by the client.
Use the first function if it’s OK to discard messages sent by the client.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论