using System.Collections.Generic; using Newtonsoft.Json; using LeanCloud.Realtime.Protocol; using LeanCloud.Storage.Internal; using LeanCloud.Storage.Internal.Codec; namespace LeanCloud.Realtime { public abstract class LCIMTypedMessage : LCIMMessage { private Dictionary customProperties; internal virtual int MessageType { get; private set; } public object this[string key] { get { if (customProperties == null) { return null; } return customProperties[key]; } set { if (customProperties == null) { customProperties = new Dictionary(); } customProperties[key] = value; } } protected LCIMTypedMessage() { } internal virtual Dictionary Encode() { Dictionary msgData = new Dictionary { { "_lctype", MessageType } }; if (customProperties != null && customProperties.Count > 0) { msgData["_lcattrs"] = LCEncoder.Encode(customProperties); } return msgData; } internal override void Decode(DirectCommand direct) { base.Decode(direct); Dictionary msgData = JsonConvert.DeserializeObject>(direct.Msg, new LCJsonConverter()); DecodeMessageData(msgData); } protected virtual void DecodeMessageData(Dictionary msgData) { MessageType = (int)msgData["_lctype"]; if (msgData.TryGetValue("_lcattrs", out object attrObj)) { customProperties = LCDecoder.Decode(attrObj) as Dictionary; } } } }