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.

113 lines
2.8 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package melody
  2. import (
  3. "github.com/gorilla/websocket"
  4. "net/http"
  5. )
  6. type handleMessageFunc func(*Session, []byte)
  7. type handleErrorFunc func(*Session, error)
  8. type handleSessionFunc func(*Session)
  9. type filterFunc func(*Session) bool
  10. type Melody struct {
  11. Config *Config
  12. Upgrader *websocket.Upgrader
  13. messageHandler handleMessageFunc
  14. messageHandlerBinary handleMessageFunc
  15. errorHandler handleErrorFunc
  16. connectHandler handleSessionFunc
  17. disconnectHandler handleSessionFunc
  18. hub *hub
  19. }
  20. // Returns a new melody instance.
  21. func New() *Melody {
  22. upgrader := &websocket.Upgrader{
  23. ReadBufferSize: 1024,
  24. WriteBufferSize: 1024,
  25. }
  26. hub := newHub()
  27. go hub.run()
  28. return &Melody{
  29. Config: newConfig(),
  30. Upgrader: upgrader,
  31. messageHandler: func(*Session, []byte) {},
  32. messageHandlerBinary: func(*Session, []byte) {},
  33. errorHandler: func(*Session, error) {},
  34. connectHandler: func(*Session) {},
  35. disconnectHandler: func(*Session) {},
  36. hub: hub,
  37. }
  38. }
  39. // Fires fn when a session connects.
  40. func (m *Melody) HandleConnect(fn func(*Session)) {
  41. m.connectHandler = fn
  42. }
  43. // Fires fn when a session disconnects.
  44. func (m *Melody) HandleDisconnect(fn func(*Session)) {
  45. m.disconnectHandler = fn
  46. }
  47. // Callback when a message comes in.
  48. func (m *Melody) HandleMessage(fn func(*Session, []byte)) {
  49. m.messageHandler = fn
  50. }
  51. // Callback when a binary message comes in.
  52. func (m *Melody) HandleMessageBinary(fn func(*Session, []byte)) {
  53. m.messageHandlerBinary = fn
  54. }
  55. // Fires when a session has an error.
  56. func (m *Melody) HandleError(fn func(*Session, error)) {
  57. m.errorHandler = fn
  58. }
  59. // Handles a http request and upgrades it to a websocket.
  60. func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) {
  61. conn, err := m.Upgrader.Upgrade(w, r, nil)
  62. if err != nil {
  63. m.errorHandler(nil, err)
  64. return
  65. }
  66. session := newSession(m.Config, conn, r)
  67. m.hub.register <- session
  68. go m.connectHandler(session)
  69. go session.writePump(m.errorHandler)
  70. session.readPump(m.messageHandler, m.messageHandlerBinary, m.errorHandler)
  71. m.hub.unregister <- session
  72. go m.disconnectHandler(session)
  73. }
  74. // Broadcasts a message to all sessions.
  75. func (m *Melody) Broadcast(msg []byte) {
  76. message := &envelope{t: websocket.TextMessage, msg: msg}
  77. m.hub.broadcast <- message
  78. }
  79. // Broadcasts a message to all sessions that fn returns true for.
  80. func (m *Melody) BroadcastFilter(msg []byte, fn func(*Session) bool) {
  81. message := &envelope{t: websocket.TextMessage, msg: msg, filter: fn}
  82. m.hub.broadcast <- message
  83. }
  84. // Broadcasts a message to all sessions except session `s`.
  85. func (m *Melody) BroadcastOthers(msg []byte, s *Session) {
  86. m.BroadcastFilter(msg, func(q *Session) bool {
  87. return s != q
  88. })
  89. }