* LCRTMRouter.cs: chore: 支持消息带自定义属性

* LCIMMessage.cs:
* Program.cs:
* LCIMTypedMessage.cs:
oneRain 2020-03-19 11:04:37 +08:00
parent d109ccef65
commit 87d481f41d
4 changed files with 44 additions and 13 deletions

View File

@ -1,7 +1,7 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Net.Http; using System.Net.Http;
using LeanCloud; using LeanCloud.Storage.Internal;
using LeanCloud.Common; using LeanCloud.Common;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -36,7 +36,7 @@ namespace LeanCloud.Realtime.Internal.Router {
response.Dispose(); response.Dispose();
LCHttpUtils.PrintResponse(response, resultString); LCHttpUtils.PrintResponse(response, resultString);
rtmServer = JsonConvert.DeserializeObject<LCRTMServer>(resultString); rtmServer = JsonConvert.DeserializeObject<LCRTMServer>(resultString, new LCJsonConverter());
return rtmServer; return rtmServer;
} }

View File

@ -75,8 +75,7 @@ namespace LeanCloud.Realtime {
get; set; get; set;
} }
public LCIMMessage() { internal LCIMMessage() {
} }
internal virtual void Decode(DirectCommand direct) { internal virtual void Decode(DirectCommand direct) {

View File

@ -2,21 +2,42 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using LeanCloud.Realtime.Protocol; using LeanCloud.Realtime.Protocol;
using LeanCloud.Storage.Internal; using LeanCloud.Storage.Internal;
using LeanCloud.Storage.Internal.Codec;
namespace LeanCloud.Realtime { namespace LeanCloud.Realtime {
public abstract class LCIMTypedMessage : LCIMMessage { public abstract class LCIMTypedMessage : LCIMMessage {
protected LCIMTypedMessage() { private Dictionary<string, object> customProperties;
}
internal virtual int MessageType { internal virtual int MessageType {
get; private set; get; private set;
} }
public object this[string key] {
get {
if (customProperties == null) {
return null;
}
return customProperties[key];
}
set {
if (customProperties == null) {
customProperties = new Dictionary<string, object>();
}
customProperties[key] = value;
}
}
protected LCIMTypedMessage() {
}
internal virtual Dictionary<string, object> Encode() { internal virtual Dictionary<string, object> Encode() {
return new Dictionary<string, object> { Dictionary<string, object> msgData = new Dictionary<string, object> {
{ "_lctype", MessageType } { "_lctype", MessageType }
}; };
if (customProperties != null && customProperties.Count > 0) {
msgData["_lcattrs"] = LCEncoder.Encode(customProperties);
}
return msgData;
} }
internal override void Decode(DirectCommand direct) { internal override void Decode(DirectCommand direct) {
@ -27,6 +48,9 @@ namespace LeanCloud.Realtime {
protected virtual void DecodeMessageData(Dictionary<string, object> msgData) { protected virtual void DecodeMessageData(Dictionary<string, object> msgData) {
MessageType = (int)msgData["_lctype"]; MessageType = (int)msgData["_lctype"];
if (msgData.TryGetValue("_lcattrs", out object attrObj)) {
customProperties = LCDecoder.Decode(attrObj) as Dictionary<string, object>;
}
} }
} }
} }

View File

@ -57,6 +57,11 @@ namespace RealtimeConsole {
world.OnMessageReceived = (conv, message) => { world.OnMessageReceived = (conv, message) => {
Console.WriteLine(message); Console.WriteLine(message);
if (message is LCIMTypedMessage typedMessage) {
Console.WriteLine(typedMessage["k1"]);
Console.WriteLine(typedMessage["k2"]);
Console.WriteLine(typedMessage["k3"]);
}
}; };
//LCIMTextMessage textMessage = new LCIMTextMessage("hello, world"); //LCIMTextMessage textMessage = new LCIMTextMessage("hello, world");
@ -76,8 +81,11 @@ namespace RealtimeConsole {
// Console.WriteLine(member.MemberId); // Console.WriteLine(member.MemberId);
//} //}
//LCIMTextMessage textMessage = new LCIMTextMessage("hello, world"); LCIMTextMessage textMessage = new LCIMTextMessage("hello, world");
//await conversation.Send(textMessage); textMessage["k1"] = 123;
textMessage["k2"] = "abc";
textMessage["k3"] = true;
await conversation.Send(textMessage);
//LCFile file = new LCFile("avatar", "../../../Storage.Test/assets/hello.png"); //LCFile file = new LCFile("avatar", "../../../Storage.Test/assets/hello.png");
//file.MetaData["width"] = 225; //file.MetaData["width"] = 225;
@ -87,9 +95,9 @@ namespace RealtimeConsole {
//LCIMImageMessage imageMessage = new LCIMImageMessage(file); //LCIMImageMessage imageMessage = new LCIMImageMessage(file);
//await conversation.Send(imageMessage); //await conversation.Send(imageMessage);
LCGeoPoint location = new LCGeoPoint(11, 12); //LCGeoPoint location = new LCGeoPoint(11, 12);
LCIMLocationMessage locationMessage = new LCIMLocationMessage(location); //LCIMLocationMessage locationMessage = new LCIMLocationMessage(location);
await conversation.Send(locationMessage); //await conversation.Send(locationMessage);
} }
} }
} }