Gorilla Websocket手动关闭客户端连接

时间:2018-08-08 14:08:10

标签: go websocket gorilla

我尝试关闭连接手册(使用Dark WebSocket Terminal),但是客户端告诉我1005(无状态接收)
服务器:

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
    }
}

它不起作用,并显示预期的输出,如下所示:

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

我应该怎么做?

1 个答案:

答案 0 :(得分:0)

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

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