forked from mirror/websocket
Improve chat example.
- Log all handshake errors. - Prevent double close on send channel.
This commit is contained in:
parent
d2dc86f575
commit
bc19d3d337
|
@ -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)
|
||||
}
|
||||
return
|
||||
}
|
||||
c := &connection{send: make(chan []byte, 256), ws: ws}
|
||||
|
|
|
@ -33,8 +33,10 @@ func (h *hub) run() {
|
|||
case c := <-h.register:
|
||||
h.connections[c] = true
|
||||
case c := <-h.unregister:
|
||||
if _, ok := h.connections[c]; ok {
|
||||
delete(h.connections, c)
|
||||
close(c.send)
|
||||
}
|
||||
case m := <-h.broadcast:
|
||||
for c := range h.connections {
|
||||
select {
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue