forked from olahol/melody
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhub.go
42 lines (39 loc) · 753 Bytes
/
hub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package melody
type hub struct {
sessions map[*Session]bool
broadcast chan *envelope
register chan *Session
unregister chan *Session
}
func newHub() *hub {
return &hub{
sessions: make(map[*Session]bool),
broadcast: make(chan *envelope),
register: make(chan *Session),
unregister: make(chan *Session),
}
}
func (h *hub) run() {
for {
select {
case s := <-h.register:
h.sessions[s] = true
case s := <-h.unregister:
if _, ok := h.sessions[s]; ok {
delete(h.sessions, s)
close(s.output)
s.conn.Close()
}
case m := <-h.broadcast:
for s := range h.sessions {
if m.filter != nil {
if m.filter(s) {
go s.writeMessage(m)
}
} else {
go s.writeMessage(m)
}
}
}
}
}