123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- package server
- import (
- "encoding"
- "encoding/json"
- "github.com/mitchellh/mapstructure"
- )
- type MessageType = int8
- type ChatingContentType = uint8
- const (
- MessageTypeError MessageType = -1 // MessageTypeError 错误消息,当服务器出现错误时,会发送此消息
- MessageTypePingPong MessageType = 1 // MessageTypePingPong ping pong 消息
- MessageTypeEmpty MessageType = 2 // MessageTypeEmpty 空消息
- MessageTypeUpChating MessageType = 30 // MessageTypeUpChating 语聊消息
- MessageTypeDownChating MessageType = 31 // MessageTypeDownChating 语聊消息
- MessageTypeNotification MessageType = 40 // MessageTypeNotification 通知消息
- )
- const (
- ChatingContentTypeText ChatingContentType = 1 + iota // ChatingContentTypeText 语聊文字消息
- ChatingContentTypeMetadata // ChatingContentTypeMetadata 媒体文件消息
- )
- var _ encoding.BinaryMarshaler = (*Message)(nil)
- var _ encoding.BinaryUnmarshaler = (*Message)(nil)
- type Message struct {
- Type MessageType `json:"type"`
- RequestId string `json:"requestId"` // RequestId 当前消息的Id
- Content any `json:"content"` // Content 内容
- Receiver string `json:"receiver,omitempty"` // Receiver 消息接受者
- }
- type FirebaseMessage struct {
- token string
- message *Message
- // clt *Client
- }
- type (
- // ChatingContent to client chating message
- ChatingContent struct {
- MessageId string `json:"messageId"`
- SessionId string `json:"sessionId"`
- Receiver string `json:"receiver"`
- PayloadType ChatingContentType `json:"payloadType"`
- Payload any `json:"payload"`
- }
- // NotificationContent to client notice message
- NotificationContent struct {
- ID int `json:"id"`
- Payload map[string]interface{} `json:"payload"`
- }
- )
- type (
- // ContentText received a text message from the client
- ContentText struct {
- Raw string `json:"raw"`
- }
- // ContentMetadata received media file message from client
- ContentMetadata struct {
- Url string `json:"url"` // Url 文件地址
- FileType string `json:"fileType"` // FileType 文件类型
- Duration uint `json:"duration"` // Duration 视频/语音时长
- }
- // ContentReply content to be replied to after receiving the message
- ContentReply struct {
- MessageId string `json:"messageId"`
- }
- // ContentError an error occurred while processing the message
- ContentError struct {
- Err string `json:"err"`
- }
- )
- func (m *Message) UnmarshalBinary(data []byte) error {
- return json.Unmarshal(data, m)
- }
- func (m *Message) MarshalBinary() (data []byte, err error) {
- return json.Marshal(m)
- }
- func (m *Message) IsNeedReply() bool {
- return m.RequestId != ""
- }
- func (m *Message) String() string {
- buf, _ := m.MarshalBinary()
- return string(buf)
- }
- func deserializeMessage(bytes []byte) *Message {
- if len(bytes) == 0 {
- return emptyMessage()
- }
- var message Message
- _ = json.Unmarshal(bytes, &message)
- switch message.Type {
- // Is it a chating message
- case MessageTypeUpChating, MessageTypeDownChating:
- var chatingContent ChatingContent
- mapstructure.Decode(message.Content, &chatingContent)
- message.Content = chatingContent
- // Is it a location message
- case MessageTypeNotification:
- var notificationContent NotificationContent
- mapstructure.Decode(message.Content, ¬ificationContent)
- message.Content = notificationContent
- }
- return &message
- }
- func serializationMessage(message *Message) []byte {
- // Erases some fields that are not needed for the client
- message.Receiver = ""
- // Serializable
- bytes, _ := json.Marshal(message)
- return bytes
- }
- // newReplyMessage new a to client reply message
- func newReplyMessage(message *Message) *Message {
- if message.Type == MessageTypePingPong {
- message.Content = "pong"
- }
- return message
- }
- func emptyMessage() *Message {
- return &Message{Type: MessageTypeEmpty}
- }
|