From bc19d3d33711c40f9c09d9a5de291a62e8feab29 Mon Sep 17 00:00:00 2001 From: Gary Burd Date: Fri, 27 Jun 2014 13:08:22 -0700 Subject: [PATCH] Improve chat example. - Log all handshake errors. - Prevent double close on send channel. --- examples/chat/conn.go | 4 +--- examples/chat/hub.go | 6 ++++-- examples/chat/main.go | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/chat/conn.go b/examples/chat/conn.go index 3dd0d9a..916bada 100644 --- a/examples/chat/conn.go +++ b/examples/chat/conn.go @@ -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} diff --git a/examples/chat/hub.go b/examples/chat/hub.go index 628366e..449ba75 100644 --- a/examples/chat/hub.go +++ b/examples/chat/hub.go @@ -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 { diff --git a/examples/chat/main.go b/examples/chat/main.go index 13320b0..3c4448d 100644 --- a/examples/chat/main.go +++ b/examples/chat/main.go @@ -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")