dop20345
dop20345
2016-03-29 14:34

通过消息关闭Websocket连接

已采纳

How is it possible to close a websocket connection and pass it a message / code?

The docs only define func (ws *Conn) Close() error without any arguments

I would like to receive the event from JavaScript like this:

websocket.onclose = function(event) {
  console.log(event);
};

I am using golang.org/x/net/websocket

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doujing2497 doujing2497 5年前

    Send a close message before closing the connection:

     cm := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "add your message here")
     if err := c.WriteMessage(websocket.CloseMessage, cm); err != nil {
         // handle error
     }
     c.Close()
    

    It is not possible to specify the close message with the golang.org/x/net/websocket package.

    点赞 评论 复制链接分享

相关推荐