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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置