websocket.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package websocket
  2. import (
  3. "github.com/olahol/melody"
  4. "net/http"
  5. )
  6. const (
  7. groupIDKey = "group-id"
  8. )
  9. type HandleConnectFunc func(groupID string)
  10. type HandleDisconnectFunc func(groupID string)
  11. type HandleErrorFunc func(groupID string, err error)
  12. type HandleCloseFunc func(groupID string, i int, s string) error
  13. type HandlePongFunc func(groupID string)
  14. type HandleMessageFunc func(groupID string, message []byte)
  15. var managerInstance *Manager
  16. func Init() {
  17. if managerInstance == nil {
  18. melodyInstance := melody.New()
  19. melodyInstance.Upgrader.CheckOrigin = func(r *http.Request) bool { return true }
  20. managerInstance = &Manager{melodyInstance: melodyInstance}
  21. }
  22. }
  23. func Destroy() {
  24. if managerInstance != nil {
  25. err := managerInstance.melodyInstance.Close()
  26. if err != nil {
  27. panic(err)
  28. }
  29. }
  30. managerInstance = nil
  31. }
  32. func GetInstance() *Manager {
  33. return managerInstance
  34. }
  35. type Manager struct {
  36. melodyInstance *melody.Melody
  37. }
  38. func (m *Manager) HandleConnect(handleConnectFunc HandleConnectFunc) {
  39. m.melodyInstance.HandleConnect(func(session *melody.Session) {
  40. if handleConnectFunc != nil {
  41. handleConnectFunc(session.Keys[groupIDKey].(string))
  42. }
  43. })
  44. }
  45. func (m *Manager) HandleDisconnect(handleDisconnectFunc HandleDisconnectFunc) {
  46. m.melodyInstance.HandleDisconnect(func(session *melody.Session) {
  47. if handleDisconnectFunc != nil {
  48. handleDisconnectFunc(session.Keys[groupIDKey].(string))
  49. }
  50. })
  51. }
  52. func (m *Manager) HandleError(handleErrorFunc HandleErrorFunc) {
  53. m.melodyInstance.HandleError(func(session *melody.Session, err error) {
  54. if handleErrorFunc != nil {
  55. handleErrorFunc(session.Keys[groupIDKey].(string), err)
  56. }
  57. })
  58. }
  59. func (m *Manager) HandleClose(handleCloseFunc HandleCloseFunc) {
  60. m.melodyInstance.HandleClose(func(session *melody.Session, i int, s string) error {
  61. if handleCloseFunc != nil {
  62. err := handleCloseFunc(session.Keys[groupIDKey].(string), i, s)
  63. if err != nil {
  64. return err
  65. }
  66. }
  67. return nil
  68. })
  69. }
  70. func (m *Manager) HandlePong(handlePongFunc HandlePongFunc) {
  71. m.melodyInstance.HandlePong(func(session *melody.Session) {
  72. if handlePongFunc != nil {
  73. handlePongFunc(session.Keys[groupIDKey].(string))
  74. }
  75. })
  76. }
  77. func (m *Manager) HandleRequest(groupID string, w http.ResponseWriter, r *http.Request) error {
  78. err := m.melodyInstance.HandleRequestWithKeys(w, r, map[string]interface{}{groupIDKey: groupID})
  79. if err != nil {
  80. return err
  81. }
  82. return nil
  83. }
  84. func (m *Manager) HandleMessage(handleMessageFunc HandleMessageFunc) {
  85. m.melodyInstance.HandleMessage(func(session *melody.Session, bytes []byte) {
  86. if handleMessageFunc != nil {
  87. handleMessageFunc(session.Keys[groupIDKey].(string), bytes)
  88. }
  89. })
  90. }
  91. func (m *Manager) BroadCast(groupID string, msg []byte) error {
  92. return m.melodyInstance.BroadcastFilter(msg, func(session *melody.Session) bool {
  93. if session.Keys[groupIDKey] != groupID {
  94. return false
  95. }
  96. return true
  97. })
  98. }