英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论