The example Gorilla websocket directory contains the hub.go file.
https://github.com/gorilla/websocket/blob/master/examples/chat/hub.go
Here you can find a method in a type hub that does this.
func (h *hub) run() {
for {
select {
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 {
case c.send <- m:
default:
close(c.send)
delete(h.connections, c)
}
}
}
}
}
Why doesn't he just send the c.send channel directly in the latter case?
case m := <-h.broadcast:
for c := range h.connections {
c.send <- m
}
Alex source
share