Improve chat example.

- Log all handshake errors.
- Prevent double close on send channel.
This commit is contained in:
Gary Burd 2014-06-27 13:08:22 -07:00
parent d2dc86f575
commit bc19d3d337
3 changed files with 6 additions and 6 deletions

View File

@ -96,9 +96,7 @@ func serveWs(w http.ResponseWriter, r *http.Request) {
} }
ws, err := upgrader.Upgrade(w, r, nil) ws, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok { log.Println(err)
log.Println(err)
}
return return
} }
c := &connection{send: make(chan []byte, 256), ws: ws} c := &connection{send: make(chan []byte, 256), ws: ws}

View File

@ -33,8 +33,10 @@ func (h *hub) run() {
case c := <-h.register: case c := <-h.register:
h.connections[c] = true h.connections[c] = true
case c := <-h.unregister: case c := <-h.unregister:
delete(h.connections, c) if _, ok := h.connections[c]; ok {
close(c.send) delete(h.connections, c)
close(c.send)
}
case m := <-h.broadcast: case m := <-h.broadcast:
for c := range h.connections { for c := range h.connections {
select { select {

View File

@ -20,7 +20,7 @@ func serveHome(w http.ResponseWriter, r *http.Request) {
return return
} }
if r.Method != "GET" { if r.Method != "GET" {
http.Error(w, "Method nod allowed", 405) http.Error(w, "Method not allowed", 405)
return return
} }
w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("Content-Type", "text/html; charset=utf-8")