websocket 增加多分组 fork https://github.com/olahol/melody
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
753 B
42 lines
753 B
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)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|