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)
if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok {
log.Println(err)
}
log.Println(err)
return
}
c := &connection{send: make(chan []byte, 256), ws: ws}

View File

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

View File

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