|
|
@@ -20,30 +20,30 @@ type hub struct {
|
|
|
unregister chan *connection
|
|
|
}
|
|
|
|
|
|
-var h = hub{
|
|
|
+var mainHub = hub{
|
|
|
broadcast: make(chan []byte),
|
|
|
register: make(chan *connection),
|
|
|
unregister: make(chan *connection),
|
|
|
connections: make(map[*connection]bool),
|
|
|
}
|
|
|
|
|
|
-func (h *hub) run() {
|
|
|
+func (hub *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)
|
|
|
+ case c := <-hub.register:
|
|
|
+ hub.connections[c] = true
|
|
|
+ case c := <-hub.unregister:
|
|
|
+ if _, ok := hub.connections[c]; ok {
|
|
|
+ delete(hub.connections, c)
|
|
|
close(c.send)
|
|
|
}
|
|
|
- case m := <-h.broadcast:
|
|
|
- for c := range h.connections {
|
|
|
+ case m := <-hub.broadcast:
|
|
|
+ for c := range hub.connections {
|
|
|
select {
|
|
|
case c.send <- m:
|
|
|
default:
|
|
|
close(c.send)
|
|
|
- delete(h.connections, c)
|
|
|
+ delete(hub.connections, c)
|
|
|
}
|
|
|
}
|
|
|
}
|