message.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. SendTime int64 `json:"sendTime"`
  43. }
  44. // NotificationContent to client notice message
  45. NotificationContent struct {
  46. ID int `json:"id"`
  47. Payload map[string]interface{} `json:"payload"`
  48. }
  49. )
  50. type (
  51. // ContentText received a text message from the client
  52. ContentText struct {
  53. Raw string `json:"raw"`
  54. }
  55. // ContentMetadata received media file message from client
  56. ContentMetadata struct {
  57. Url string `json:"url"` // Url 文件地址
  58. FileType string `json:"fileType"` // FileType 文件类型
  59. Duration uint `json:"duration"` // Duration 视频/语音时长
  60. }
  61. // ContentReply content to be replied to after receiving the message
  62. ContentReply struct {
  63. MessageId string `json:"messageId"`
  64. }
  65. // ContentError an error occurred while processing the message
  66. ContentError struct {
  67. Err string `json:"err"`
  68. }
  69. )
  70. func (m *Message) UnmarshalBinary(data []byte) error {
  71. return json.Unmarshal(data, m)
  72. }
  73. func (m *Message) MarshalBinary() (data []byte, err error) {
  74. return json.Marshal(m)
  75. }
  76. func (m *Message) IsNeedReply() bool {
  77. return m.RequestId != ""
  78. }
  79. func (m *Message) String() string {
  80. buf, _ := m.MarshalBinary()
  81. return string(buf)
  82. }
  83. func deserializeMessage(bytes []byte) *Message {
  84. if len(bytes) == 0 {
  85. return emptyMessage()
  86. }
  87. var message Message
  88. _ = json.Unmarshal(bytes, &message)
  89. switch message.Type {
  90. // Is it a chating message
  91. case MessageTypeUpChating, MessageTypeDownChating:
  92. var chatingContent ChatingContent
  93. mapstructure.Decode(message.Content, &chatingContent)
  94. message.Content = chatingContent
  95. // Is it a location message
  96. case MessageTypeNotification:
  97. var notificationContent NotificationContent
  98. mapstructure.Decode(message.Content, &notificationContent)
  99. message.Content = notificationContent
  100. }
  101. return &message
  102. }
  103. func serializationMessage(message *Message) []byte {
  104. // Erases some fields that are not needed for the client
  105. message.Receiver = ""
  106. // Serializable
  107. bytes, _ := json.Marshal(message)
  108. return bytes
  109. }
  110. // newReplyMessage new a to client reply message
  111. func newReplyMessage(message *Message) *Message {
  112. if message.Type == MessageTypePingPong {
  113. message.Content = "pong"
  114. }
  115. return message
  116. }
  117. func emptyMessage() *Message {
  118. return &Message{Type: MessageTypeEmpty}
  119. }