try close disconnected ws #1

This commit is contained in:
Martin Geno 2016-02-25 23:30:17 +01:00
parent 45c90bdbdf
commit 5c9eff324f
2 changed files with 16 additions and 1 deletions

View File

@ -63,7 +63,16 @@ func (c *Client) listen() {
for { for {
select { select {
case msg := <-c.ch: 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)
}
} }
} }
} }

View File

@ -1,6 +1,7 @@
package websocketserver package websocketserver
import ( import (
"fmt"
"log" "log"
"net/http" "net/http"
@ -86,6 +87,11 @@ func (s *Server) Listen() {
client := NewClient(ws, s) client := NewClient(ws, s)
s.Add(client) s.Add(client)
defer func() {
s.Del(client)
err := fmt.Errorf("Client %d is disconnected.", client.id)
s.Err(err)
}()
client.Listen() client.Listen()
} }
http.Handle(s.pattern, websocket.Handler(onConnected)) http.Handle(s.pattern, websocket.Handler(onConnected))