服务器正确关闭 WebSocket 连接

huangapple go评论80阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2021年12月24日 08:29:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/70468570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定