Gorilla Websocket-读取错误:在失败的Websocket连接上重复读取

Gorilla Websocket-读取错误:在失败的Websocket连接上重复读取,go,websocket,server,gorilla,Go,Websocket,Server,Gorilla,使用gorilla websocket api for go,我如何知道客户端是否仍然连接 我现在尝试的是: func Listen(ws *websocket.Conn) { connTimeout := 3 timeLastSent := time.Now().Second() for ((timeLastSent + connTimeout) % 60) != time.Now().Second() { msg := Message{}

使用gorilla websocket api for go,我如何知道客户端是否仍然连接

我现在尝试的是:

func Listen(ws *websocket.Conn) {
    connTimeout := 3
    timeLastSent := time.Now().Second()

    for ((timeLastSent + connTimeout) % 60) != time.Now().Second() {

        msg := Message{}
        err := ws.ReadJSON(&msg)

        if err == websocket.ErrCloseSent {
            break
        } else if err != nil {
            continue
        }

        //Message recived
        EventMessage <- msg

        timeLastSent = time.Now().Second()
    }
  //Connection timed out.
    return
}
func监听(ws*websocket.Conn){
超时时间:=3
timeLastSent:=time.Now().Second()
for((timeLastSent+connTimeout)%60)!=time.Now().Second(){
消息:=消息{}
错误:=ws.ReadJSON(&msg)
如果err==websocket.ErrCloseSent{
打破
}否则,如果错误!=零{
持续
}
//收到消息

EventMessage您查看了Github repo中的示例了吗?@JohnSmith?没有,@JohnSmith我不知道这会有什么帮助:/能否解释一下?它展示了如何处理乒乓球请求。基本上,您不需要像示例中那样处理截止日期。一旦在在指定的时间范围内,您只需返回(关闭服务器上的连接)。