From 5c9eff324fae9913c107c50529c6bbad1caa848c Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Thu, 25 Feb 2016 23:30:17 +0100 Subject: [PATCH] try close disconnected ws #1 --- websocketserver/client.go | 11 ++++++++++- websocketserver/server.go | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/websocketserver/client.go b/websocketserver/client.go index 55c47d6..abda9eb 100644 --- a/websocketserver/client.go +++ b/websocketserver/client.go @@ -63,7 +63,16 @@ func (c *Client) listen() { for { select { case msg := <-c.ch: - websocket.JSON.Send(c.ws, msg) + err := websocket.JSON.Send(c.ws, msg) + if err != nil { + c.doneCh <- true + } + case gone := <-c.doneCh: + if gone { + c.server.Del(c) + err := fmt.Errorf("Client %d is disconnected.", c.id) + c.server.Err(err) + } } } } diff --git a/websocketserver/server.go b/websocketserver/server.go index d01de0e..a779825 100644 --- a/websocketserver/server.go +++ b/websocketserver/server.go @@ -1,6 +1,7 @@ package websocketserver import ( + "fmt" "log" "net/http" @@ -86,6 +87,11 @@ func (s *Server) Listen() { client := NewClient(ws, s) s.Add(client) + defer func() { + s.Del(client) + err := fmt.Errorf("Client %d is disconnected.", client.id) + s.Err(err) + }() client.Listen() } http.Handle(s.pattern, websocket.Handler(onConnected))