2018-05-15 12:57
浏览 370


Why it closed the connection directly when time out rather than call the closeHandler method?

ws.SetCloseHandler(func(code int, text string) error {
    fmt.Println("socket close")
// can not get "socket close"
stdout: "websocket: close 1006 (abnormal closure): unexpected EOF"

package: ""

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drjun1994
    drjun1994 2018-05-15 13:09

    The websocket RFC says this about status 1006:

    1006 is a reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint. It is designated for use in applications expecting a status code to indicate that the connection was closed abnormally, e.g., without sending or receiving a Close control frame.

    The websocket package documention says:

    SetCloseHandler sets the handler for close messages received from the peer

    The close handler was not called because a close message was not received. The peer closed the connection without sending a close message.

    The package documentation also says:

    The connection read methods return a CloseError when a close message is received. Most applications should handle close messages as part of their normal error handling. Applications should only set a close handler when the application must perform some action before sending a close message back to the peer.

    Because the handler in the question does not send a close message or take any other action, the close handler in the question is not needed. The handler is harmful because it does not echo the close message.

    Delete the close handler and handle close messages as part of the application's normal error handling.

    点赞 评论