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.

100 lines
2.3 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
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. errorHandler handleErrorFunc
  15. connectHandler handleSessionFunc
  16. disconnectHandler handleSessionFunc
  17. hub *hub
  18. }
  19. // Returns a new melody instance.
  20. func New() *Melody {
  21. upgrader := &websocket.Upgrader{
  22. ReadBufferSize: 1024,
  23. WriteBufferSize: 1024,
  24. }
  25. hub := newHub()
  26. go hub.run()
  27. return &Melody{
  28. Config: newConfig(),
  29. upgrader: upgrader,
  30. messageHandler: func(*Session, []byte) {},
  31. errorHandler: func(*Session, error) {},
  32. connectHandler: func(*Session) {},
  33. disconnectHandler: func(*Session) {},
  34. hub: hub,
  35. }
  36. }
  37. // Fires fn when a session connects.
  38. func (m *Melody) HandleConnect(fn func(*Session)) {
  39. m.connectHandler = fn
  40. }
  41. // Fires fn when a session disconnects.
  42. func (m *Melody) HandleDisconnect(fn func(*Session)) {
  43. m.disconnectHandler = fn
  44. }
  45. // Callback when a message comes in.
  46. func (m *Melody) HandleMessage(fn func(*Session, []byte)) {
  47. m.messageHandler = fn
  48. }
  49. // Fires when a session has an error.
  50. func (m *Melody) HandleError(fn func(*Session, error)) {
  51. m.errorHandler = fn
  52. }
  53. // Handles a http request and upgrades it to a websocket.
  54. func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) error {
  55. conn, err := m.upgrader.Upgrade(w, r, nil)
  56. if err != nil {
  57. return err
  58. }
  59. session := newSession(m.Config, conn)
  60. m.hub.register <- session
  61. go m.connectHandler(session)
  62. go session.writePump(m.errorHandler)
  63. session.readPump(m.messageHandler, m.errorHandler)
  64. m.hub.unregister <- session
  65. go m.disconnectHandler(session)
  66. return nil
  67. }
  68. // Broadcasts a message to all sessions.
  69. func (m *Melody) Broadcast(msg []byte) {
  70. message := &envelope{t: websocket.TextMessage, msg: msg}
  71. m.hub.broadcast <- message
  72. }
  73. // Broadcasts a message to all sessions that fn returns true for.
  74. func (m *Melody) BroadcastFilter(fn func(*Session) bool, msg []byte) {
  75. message := &envelope{t: websocket.TextMessage, msg: msg, filter: fn}
  76. m.hub.broadcast <- message
  77. }