try close disconnected ws #1
This commit is contained in:
parent
45c90bdbdf
commit
5c9eff324f
@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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))
|
||||||
|
Loading…
Reference in New Issue
Block a user