Gorilla Websocket手动关闭客户端连接

我尝试关闭连接手册(使用黑暗的WebSocket终端),但是客户端告诉我1005(无状态Rcvd)

服务器:</ p>

  ReadLoop:\  nfor {
mt,message,err:= c.ReadMessage()
if err!= nil {
log.Println(“ read:”,err)
log.Println(“ messageType:”,mt)

如果websocket.IsUnexpectedCloseError(err,websocket.CloseGoingAway,websocket.CloseAbnormalClosure){
log.Printf(“错误:%v”,err)
}
c.WriteMessage(websocket.CloseMessage,websocket.FormatCloseMessage( websocket.CloseNormalClosure,“”))
中断ReadLoop
}
log.Printf(“ recv:%s”,消息)

//在此处执行某项

err = c.WriteMessage( mt,message)
if err!= nil {
// WriteMessage错误
log.Println(“ write:”,err)
break ReadLoop
}
}
</ code> </ pre >

不起作用,并显示预期的输出 g以下内容:</ p>

 读取:websocket:关闭1005(无状态)
messageType:-1
错误:websocket:关闭1005(无状态)
</ code> </ pre>

我应该怎么做?</ p>
</ div>

展开原文

原文

I try to close connect manual(use Dark WebSocket Terminal),but the client tell me 1005 (No Status Rcvd)
Server:

ReadLoop:
for {
    mt, message, err := c.ReadMessage()
    if err != nil {
        log.Println("read:", err)
        log.Println("messageType:", mt)
        if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
            log.Printf("error: %v", err)
        }
        c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
        break ReadLoop
    }
    log.Printf("recv: %s", message)

    //do sth in here

    err = c.WriteMessage(mt, message)
    if err != nil {
        //WriteMessage wrong
        log.Println("write:", err)
        break ReadLoop
    }
}

It is not work and show the expected outputting the following:

read: websocket: close 1005 (no status)
messageType: -1
error: websocket: close 1005 (no status)

How should i go about this?

1个回答



对等方在不发送状态的情况下关闭了连接。 您可以通过将 websocket.CloseNoStatusReceived </ code>添加到 websocket.IsUnexpectedCloseError </ code>参数来禁止记录此状态。</ p>

 (如果是websocket)。  IsUnexpectedCloseError(err,websocket.CloseGoingAway,websocket.CloseNoStatusReceived){
log.Printf(“错误:%v”,err)
}
</ code> </ pre>
</ div>

展开原文

原文

The peer closed the connection without sending a status. You can suppress logging of this status by adding websocket.CloseNoStatusReceived to the websocket.IsUnexpectedCloseError arguments.

    if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNoStatusReceived) {
        log.Printf("error: %v", err)
    }

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐