message.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package server
  2. import (
  3. "encoding"
  4. "encoding/json"
  5. "github.com/mitchellh/mapstructure"
  6. )
  7. type MessageType = int8
  8. type ChatingContentType = uint8
  9. const (
  10. MessageTypeError MessageType = -1 // MessageTypeError 错误消息,当服务器出现错误时,会发送此消息
  11. MessageTypePingPong MessageType = 1 // MessageTypePingPong ping pong 消息
  12. MessageTypeEmpty MessageType = 2 // MessageTypeEmpty 空消息
  13. MessageTypeUpChating MessageType = 30 // MessageTypeUpChating 语聊消息
  14. MessageTypeDownChating MessageType = 31 // MessageTypeDownChating 语聊消息
  15. MessageTypeNotification MessageType = 40 // MessageTypeNotification 通知消息
  16. )
  17. const (
  18. ChatingContentTypeText ChatingContentType = 1 + iota // ChatingContentTypeText 语聊文字消息
  19. ChatingContentTypeMetadata // ChatingContentTypeMetadata 媒体文件消息
  20. )
  21. var _ encoding.BinaryMarshaler = (*Message)(nil)
  22. var _ encoding.BinaryUnmarshaler = (*Message)(nil)
  23. type Message struct {
  24. Type MessageType `json:"type"`
  25. RequestId string `json:"requestId"` // RequestId 当前消息的Id
  26. Content any `json:"content"` // Content 内容
  27. Receiver string `json:"receiver,omitempty"` // Receiver 消息接受者
  28. }
  29. type FirebaseMessage struct {
  30. token string
  31. message *Message
  32. // clt *Client
  33. }
  34. type (
  35. // ChatingContent to client chating message
  36. ChatingContent struct {
  37. MessageId string `json:"messageId"`
  38. SessionId string `json:"sessionId"`
  39. Receiver string `json:"receiver"`
  40. PayloadType ChatingContentType `json:"payloadType"`
  41. Payload any `json:"payload"`
  42. }
  43. // NotificationContent to client notice message
  44. NotificationContent struct {
  45. ID int `json:"id"`
  46. Payload map[string]interface{} `json:"payload"`
  47. }
  48. )
  49. type (
  50. // ContentText received a text message from the client
  51. ContentText struct {
  52. Raw string `json:"raw"`
  53. }
  54. // ContentMetadata received media file message from client
  55. ContentMetadata struct {
  56. Url string `json:"url"` // Url 文件地址
  57. FileType string `json:"fileType"` // FileType 文件类型
  58. Duration uint `json:"duration"` // Duration 视频/语音时长
  59. }
  60. // ContentReply content to be replied to after receiving the message
  61. ContentReply struct {
  62. MessageId string `json:"messageId"`
  63. }
  64. // ContentError an error occurred while processing the message
  65. ContentError struct {
  66. Err string `json:"err"`
  67. }
  68. )
  69. func (m *Message) UnmarshalBinary(data []byte) error {
  70. return json.Unmarshal(data, m)
  71. }
  72. func (m *Message) MarshalBinary() (data []byte, err error) {
  73. return json.Marshal(m)
  74. }
  75. func (m *Message) IsNeedReply() bool {
  76. return m.RequestId != ""
  77. }
  78. func (m *Message) String() string {
  79. buf, _ := m.MarshalBinary()
  80. return string(buf)
  81. }
  82. func deserializeMessage(bytes []byte) *Message {
  83. if len(bytes) == 0 {
  84. return emptyMessage()
  85. }
  86. var message Message
  87. _ = json.Unmarshal(bytes, &message)
  88. switch message.Type {
  89. // Is it a chating message
  90. case MessageTypeUpChating, MessageTypeDownChating:
  91. var chatingContent ChatingContent
  92. mapstructure.Decode(message.Content, &chatingContent)
  93. message.Content = chatingContent
  94. // Is it a location message
  95. case MessageTypeNotification:
  96. var notificationContent NotificationContent
  97. mapstructure.Decode(message.Content, &notificationContent)
  98. message.Content = notificationContent
  99. }
  100. return &message
  101. }
  102. func serializationMessage(message *Message) []byte {
  103. // Erases some fields that are not needed for the client
  104. message.Receiver = ""
  105. // Serializable
  106. bytes, _ := json.Marshal(message)
  107. return bytes
  108. }
  109. // newReplyMessage new a to client reply message
  110. func newReplyMessage(message *Message) *Message {
  111. if message.Type == MessageTypePingPong {
  112. message.Content = "pong"
  113. }
  114. return message
  115. }
  116. func emptyMessage() *Message {
  117. return &Message{Type: MessageTypeEmpty}
  118. }