douxieqiu0651 2018-05-15 12:57
浏览 933
已采纳

为什么它在超时时直接关闭连接而不是调用closeHandler方法?

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: "github.com/gorilla/websocket"

  • 写回答

1条回答 默认 最新

  • 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog