推送
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.

111 lines
2.2 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package hasaki_push
  2. import (
  3. "git.ouxuan.net/tommy/melody"
  4. "sync"
  5. )
  6. var PushMessageSpanSecond = 5
  7. func Send(sender int, receiver int, messageType string, Content interface{}, retries ...int) error {
  8. retry := 0
  9. if len(retries) > 0 {
  10. retry = retries[0]
  11. }
  12. return add(&PushMessage{
  13. Type: messageType,
  14. Sender: sender,
  15. Receiver: receiver,
  16. RetryNumber: retry,
  17. SpanSecond: PushMessageSpanSecond,
  18. Content: Content,
  19. Status: NotSend,
  20. })
  21. }
  22. func SetStatus(uniqueId string, status MessageStatus) error {
  23. return setStatus(uniqueId, status)
  24. }
  25. type Message struct {
  26. UniqueId string `json:"unique_id"`
  27. Type string `json:"type"`
  28. Content interface{} `json:"content"`
  29. }
  30. var messageHandlerMap sync.Map
  31. type MessageHandler func(session *melody.Session, message []byte)
  32. func SetMessageHandler(messageType string, f MessageHandler) {
  33. messageHandlerMap.Store(messageType, f)
  34. }
  35. // session: 身份 - 某个点
  36. type Node struct {
  37. *melody.Melody
  38. sessions map[string]map[string]*Session
  39. mutex sync.Mutex
  40. }
  41. func NewNode() *Node {
  42. return &Node{
  43. Melody: melody.New(),
  44. sessions: map[string]map[string]*Session{},
  45. mutex: sync.Mutex{},
  46. }
  47. }
  48. func (n *Node) GetMelody() *melody.Melody {
  49. return n.Melody
  50. }
  51. func (n *Node) SetSession(uid string, session *Session) {
  52. n.mutex.Lock()
  53. if n.sessions == nil {
  54. n.sessions = map[string]map[string]*Session{}
  55. }
  56. //n.sessions[uid] = append(n.sessions[uid], session)
  57. n.mutex.Unlock()
  58. }
  59. // 身份
  60. type IdentityType string
  61. const (
  62. Unknown IdentityType = "unknown" // 未知
  63. Pad IdentityType = "pad" // 平板
  64. Admin IdentityType = "admin" // 管理员
  65. User IdentityType = "user" // 用户
  66. Coach IdentityType = "coach" // 教练
  67. )
  68. type DeviceType string
  69. const (
  70. NoDevice DeviceType = "no_device"
  71. WebServer DeviceType = "web"
  72. Android DeviceType = "android"
  73. IosPhone DeviceType = "ios"
  74. WechatMini DeviceType = "wechat_mini"
  75. )
  76. type Session struct {
  77. *melody.Session
  78. Identity IdentityType
  79. Device DeviceType
  80. UserId int
  81. }
  82. func NewSession(session *melody.Session) *Session {
  83. return &Session{
  84. Identity: Unknown,
  85. Device: WechatMini,
  86. Session: session,
  87. UserId: 0,
  88. }
  89. }
  90. func (s *Session) SetUid() {
  91. }