英文:
gorilla golang websocket.IsUnexpectedCloseError not working correctly under windows?
问题
我有一个例程,试图检测读取时的 WebSocket 错误是否为 UnexpectedCloseError。
以下是代码...
有一个变量:
myWebsocket *websocket.Conn
我的方法是:
messageType, data, err := myWebsocket.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err) {
log.Debugf("WebsocketReadDataMessage: unexpected close error: %v", err)
} else {
log.Warnf("EXTRANGE!!!!: WebsocketReadDataMessage: error: %v", err)
}
return err
}
在 Windows 下运行此代码时,有时 WebSocket 会关闭,但对 websocket.IsUnexpectedCloseError 的调用会返回 "false",并且我会收到一个带有文本 "EXTRANGE!!!!: WebsocketReadDataMessage: error: wsarecv: An existing connection was forcibly closed by the remote host." 的错误。
搜索 "wsarecv",我发现它是一个 winsock2 函数:https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsarecv
为什么 IsUnexpectedCloseError 不将此错误视为 "UnexpectedCloseError"?这是一个 bug 还是我做错了什么?我该怎么办才能解决这个问题?
英文:
I have a routine that tries to detect if a websocket error while reading is an UnexpectedCloseError.
This is the code...
having a variable:
myWebsocket *websocket.Conn
my method is:
messageType, data, err := myWebsocket.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err) {
log.Debugf("WebsocketReadDataMessage: unexpected close error: %v", err)
} else {
log.Warnf("EXTRANGE!!!!: WebsocketReadDataMessage: error: %v", err)
}
return err
}
Running this code under windows sometimes the websocket closes but the call to websocket.IsUnexpectedCloseError returns "false" and I get an error with the text "EXTRANGE!!!!: WebsocketReadDataMessage: error: wsarecv: An existing connection was forcibly closed by the remote host."
Searching for "wsarecv" and I have seen that it is a winsock2 function: https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsarecv
Why IsUnexpectedCloseError doesn't treat this error as an "UnexpectedCloseError"? Is it a bug or maybe I'm doing something wrong? What can I do to
答案1
得分: 1
函数websocket.IsUnexpectedCloseError()
在Windows上工作正常。该函数中没有与平台相关的代码。
函数websocket.IsUnexpectedCloseError返回一个布尔值,指示错误是否为*websocket.CloseError
,且其代码不在预期代码列表中。*websocket.CloseError
表示从对等方发送的关闭消息。*websocket.CloseError
并不表示底层网络连接已关闭。根据文档中的示例,该函数旨在帮助调试。
听起来你的目标是确定连接何时不再可用。在ReadMessage()
返回任何错误后,连接将不再可用。在任何错误上关闭连接并重新连接。
messageType, data, err := myWebsocket.ReadMessage()
if err != nil {
myWebsocket.Close()
return err
}
英文:
The function websocket.IsUnexpectedCloseError()
works correctly on Windows. There is no platform dependent code in the function.
The function websocket.IsUnexpectedCloseError returns a boolean indicating whether the error is a *websocket.CloseError
with a code not in the list of expected codes. A *websocket.CloseError
represents a close message sent from the peer. A *websocket.CloseError
does not indicate that the underlying network connection was closed. According the example in the documentation, the function is intended to aid debugging.
It sounds like your goal is to determine when the connection is no longer useable. The connection is not useable after ReadMessage()
returns any error. Close the connection and reconnect on any error.
messageType, data, err := myWebsocket.ReadMessage()
if err != nil {
myWebsocket.Close()
return err
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论