|
@@ -26,14 +26,14 @@ const (
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
- NotificationTypeDeviceStatusChange = 100 // NotificationTypeDeviceStatusChange 设备绑定状态变更通知
|
|
|
- NotificationTypeAskLocation = 111 // NotificationTypeAskLocation 询问设备位置
|
|
|
- NotificationTypeChangedContacts = 130 // NotificationTypeChangedContacts 联系人变动通知
|
|
|
- NotificationTypeCreatedFriend = 120 // NotificationTypeCreatedFriend 通知添加好友结果通知
|
|
|
- NotificationTypeChangedAlarmClock = 140 // NotificationTypeChangedAlarmClock 闹钟变更通知
|
|
|
- NotificationTypeChangedSchoolMode = 141 // NotificationTypeChangedSchoolMode 上课禁用变更通知
|
|
|
- NotificationTypeDeviceShutdown = 150 // NotificationTypeDeviceShutdown 设备关机通知
|
|
|
- NotificationTypeDeviceReboot = 160 // NotificationTypeDeviceReboot 设备重启通知
|
|
|
+ NotificationTypeDeviceStatusChange int = 100 // NotificationTypeDeviceStatusChange 设备绑定状态变更通知
|
|
|
+ NotificationTypeAskLocation int = 111 // NotificationTypeAskLocation 询问设备位置
|
|
|
+ NotificationTypeChangedContacts int = 130 // NotificationTypeChangedContacts 联系人变动通知
|
|
|
+ NotificationTypeCreatedFriend int = 120 // NotificationTypeCreatedFriend 通知添加好友结果通知
|
|
|
+ NotificationTypeChangedAlarmClock int = 140 // NotificationTypeChangedAlarmClock 闹钟变更通知
|
|
|
+ NotificationTypeChangedSchoolMode int = 141 // NotificationTypeChangedSchoolMode 上课禁用变更通知
|
|
|
+ NotificationTypeDeviceShutdown int = 150 // NotificationTypeDeviceShutdown 设备关机通知
|
|
|
+ NotificationTypeDeviceReboot int = 160 // NotificationTypeDeviceReboot 设备重启通知
|
|
|
)
|
|
|
|
|
|
// var _ encoding.BinaryMarshaler = (*Message)(nil)
|
|
@@ -118,32 +118,51 @@ type metadata struct {
|
|
|
Duration uint `json:"duration" mapstructure:""` // Duration 视频/语音时长
|
|
|
}
|
|
|
|
|
|
-type ErrMessage struct {
|
|
|
+type Err struct {
|
|
|
MessageImpl
|
|
|
|
|
|
Content string `json:"content"` // 错误内容或者提示
|
|
|
}
|
|
|
|
|
|
// Data implements Message.
|
|
|
-func (c *ErrMessage) Data() []byte {
|
|
|
+func (c *Err) Data() []byte {
|
|
|
data, _ := json.Marshal(c)
|
|
|
return data
|
|
|
}
|
|
|
|
|
|
-var _ Message = (*HeartbeatMessage)(nil)
|
|
|
+var _ Message = (*Heartbeat)(nil)
|
|
|
|
|
|
-type HeartbeatMessage struct {
|
|
|
+type Heartbeat struct {
|
|
|
MessageImpl
|
|
|
|
|
|
Content string `json:"content"`
|
|
|
}
|
|
|
|
|
|
// Data implements Message.
|
|
|
-func (h *HeartbeatMessage) Data() []byte {
|
|
|
+func (h *Heartbeat) Data() []byte {
|
|
|
data, _ := json.Marshal(h)
|
|
|
return data
|
|
|
}
|
|
|
|
|
|
+// 通知消息
|
|
|
+type Notification struct {
|
|
|
+ MessageImpl
|
|
|
+ Content *NotificationContent `json:"context"`
|
|
|
+}
|
|
|
+
|
|
|
+type NotificationContent struct {
|
|
|
+ ID int `json:"id"` // ID 通知消息的ID, 不同的通知会有不同的ID, 可以 NotificationType
|
|
|
+ Receiver string `json:"receiver"`
|
|
|
+ Sender string `json:"sender"`
|
|
|
+ Payload map[string]interface{} `json:"payload,omitempty"`
|
|
|
+}
|
|
|
+
|
|
|
+// Data implements Message.
|
|
|
+func (n *Notification) Data() []byte {
|
|
|
+ data, _ := json.Marshal(n)
|
|
|
+ return data
|
|
|
+}
|
|
|
+
|
|
|
type messageOption func(msg Message)
|
|
|
|
|
|
func deserializeMessage(data []byte, opts ...messageOption) Message {
|
|
@@ -160,9 +179,14 @@ func deserializeMessage(data []byte, opts ...messageOption) Message {
|
|
|
_ = json.Unmarshal(data, &chating)
|
|
|
msg = &chating
|
|
|
case MessageTypePingPong:
|
|
|
- var heartbeat HeartbeatMessage
|
|
|
+ var heartbeat Heartbeat
|
|
|
_ = json.Unmarshal(data, &heartbeat)
|
|
|
msg = &heartbeat
|
|
|
+
|
|
|
+ case MessageTypeNotification:
|
|
|
+ var notification Notification
|
|
|
+ _ = json.Unmarshal(data, ¬ification)
|
|
|
+ msg = ¬ification
|
|
|
}
|
|
|
|
|
|
for _, opt := range opts {
|
|
@@ -187,29 +211,36 @@ func serializeMessage(message Message) []byte {
|
|
|
}
|
|
|
|
|
|
func serializePayload(payloadType PayloadType, m map[string]interface{}) []byte {
|
|
|
- if payloadType == PayloadTypeText {
|
|
|
+ switch payloadType {
|
|
|
+ case PayloadTypeText:
|
|
|
var text text
|
|
|
_ = mapstructure.Decode(m, &text)
|
|
|
data, _ := json.Marshal(text)
|
|
|
return data
|
|
|
- }
|
|
|
-
|
|
|
- if payloadType == PayloadTypeMetadata {
|
|
|
+ case PayloadTypeMetadata:
|
|
|
var metadata metadata
|
|
|
_ = mapstructure.Decode(m, &metadata)
|
|
|
data, _ := json.Marshal(metadata)
|
|
|
return data
|
|
|
+
|
|
|
+ default:
|
|
|
+ data, _ := json.Marshal(m)
|
|
|
+ return data
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- return nil
|
|
|
+func deserializePayload(data []byte) map[string]interface{} {
|
|
|
+ var res map[string]interface{}
|
|
|
+ _ = json.Unmarshal(data, &res)
|
|
|
+ return res
|
|
|
}
|
|
|
|
|
|
func newErrorMessage(rid string, err error) Message {
|
|
|
- return &ErrMessage{MessageImpl: MessageImpl{Type: MessageTypeError, RId: rid}, Content: err.Error()}
|
|
|
+ return &Err{MessageImpl: MessageImpl{Type: MessageTypeError, RId: rid}, Content: err.Error()}
|
|
|
}
|
|
|
|
|
|
func newPongMessage(rid string) Message {
|
|
|
- return &HeartbeatMessage{MessageImpl: MessageImpl{Type: MessageTypePingPong, RId: rid}, Content: "pong"}
|
|
|
+ return &Heartbeat{MessageImpl: MessageImpl{Type: MessageTypePingPong, RId: rid}, Content: "pong"}
|
|
|
}
|
|
|
|
|
|
// type EncodedMessage struct {
|