使用Gorilla FormatCloseMessage客户端接收到错误的代码。

huangapple go评论77阅读模式
英文:

Using Gorilla FormatCloseMessage client receives wrong code

问题

我正在关闭一个代码为1008的WS连接。

cm := websocket.FormatCloseMessage(websocket.ClosePolicyViolation, msg)
if err := c.conn.WriteMessage(websocket.CloseMessage, cm); err != nil {
	log.Error(err)
}
err = c.conn.Close()

然而,当使用WS库接收消息时,它显示代码为1006。我做错了什么?

英文:

I am closing a WS connection with the code 1008.

cm := websocket.FormatCloseMessage(websocket.ClosePolicyViolation, msg)
if err := c.conn.WriteMessage(websocket.CloseMessage, cm); err != nil {
	log.Error(err)
}
err = c.conn.Close()

However when receiving the message with WS library, it says the code is 1006. What am I doing wrong?

答案1

得分: 0

由于在发送消息后立即关闭连接,关闭握手未完成。修复的一种方法是添加一个sleep调用。不过,最好的方法是正确实现关闭握手。

英文:

As the connection is instantly closed after sending the message the close handshake is not completed. One way to fix it is by adding a sleep call. Though it would be better to implement the close handshake correctly.

huangapple
  • 本文由 发表于 2017年6月1日 01:03:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/44290770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定