Explorar el Código

Improve chat example.

- Log all handshake errors.
- Prevent double close on send channel.
Gary Burd hace 11 años
padre
commit
bc19d3d337
Se han modificado 3 ficheros con 6 adiciones y 6 borrados
  1. 1 3
      examples/chat/conn.go
  2. 4 2
      examples/chat/hub.go
  3. 1 1
      examples/chat/main.go

+ 1 - 3
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}

+ 4 - 2
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 {

+ 1 - 1
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")