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
747 B

10 years ago
  1. package melody
  2. type hub struct {
  3. sessions map[*Session]bool
  4. broadcast chan *envelope
  5. register chan *Session
  6. unregister chan *Session
  7. }
  8. func newHub() *hub {
  9. return &hub{
  10. sessions: make(map[*Session]bool),
  11. broadcast: make(chan *envelope),
  12. register: make(chan *Session),
  13. unregister: make(chan *Session),
  14. }
  15. }
  16. func (h *hub) run() {
  17. for {
  18. select {
  19. case s := <-h.register:
  20. h.sessions[s] = true
  21. case s := <-h.unregister:
  22. if _, ok := h.sessions[s]; ok {
  23. delete(h.sessions, s)
  24. close(s.output)
  25. s.Conn.Close()
  26. }
  27. case m := <-h.broadcast:
  28. for s := range h.sessions {
  29. if m.filter != nil {
  30. if m.filter(s) {
  31. s.writeMessage(m)
  32. }
  33. } else {
  34. s.writeMessage(m)
  35. }
  36. }
  37. }
  38. }
  39. }