From 87d481f41dd281d4637dea95719100f966998c19 Mon Sep 17 00:00:00 2001 From: oneRain Date: Thu, 19 Mar 2020 11:04:37 +0800 Subject: [PATCH] =?UTF-8?q?*=20LCRTMRouter.cs:=20chore:=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=B6=88=E6=81=AF=E5=B8=A6=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * LCIMMessage.cs: * Program.cs: * LCIMTypedMessage.cs: --- Realtime/Internal/Router/LCRTMRouter.cs | 4 ++-- Realtime/Message/LCIMMessage.cs | 3 +-- Realtime/Message/LCIMTypedMessage.cs | 32 +++++++++++++++++++++---- Test/RealtimeConsole/Program.cs | 18 ++++++++++---- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/Realtime/Internal/Router/LCRTMRouter.cs b/Realtime/Internal/Router/LCRTMRouter.cs index 5d78576..5e09a39 100644 --- a/Realtime/Internal/Router/LCRTMRouter.cs +++ b/Realtime/Internal/Router/LCRTMRouter.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; using System.Net.Http; -using LeanCloud; +using LeanCloud.Storage.Internal; using LeanCloud.Common; using Newtonsoft.Json; @@ -36,7 +36,7 @@ namespace LeanCloud.Realtime.Internal.Router { response.Dispose(); LCHttpUtils.PrintResponse(response, resultString); - rtmServer = JsonConvert.DeserializeObject(resultString); + rtmServer = JsonConvert.DeserializeObject(resultString, new LCJsonConverter()); return rtmServer; } diff --git a/Realtime/Message/LCIMMessage.cs b/Realtime/Message/LCIMMessage.cs index 07f8cdb..7805eba 100644 --- a/Realtime/Message/LCIMMessage.cs +++ b/Realtime/Message/LCIMMessage.cs @@ -75,8 +75,7 @@ namespace LeanCloud.Realtime { get; set; } - public LCIMMessage() { - + internal LCIMMessage() { } internal virtual void Decode(DirectCommand direct) { diff --git a/Realtime/Message/LCIMTypedMessage.cs b/Realtime/Message/LCIMTypedMessage.cs index 1f595e0..566c5e6 100644 --- a/Realtime/Message/LCIMTypedMessage.cs +++ b/Realtime/Message/LCIMTypedMessage.cs @@ -2,21 +2,42 @@ using Newtonsoft.Json; using LeanCloud.Realtime.Protocol; using LeanCloud.Storage.Internal; +using LeanCloud.Storage.Internal.Codec; namespace LeanCloud.Realtime { public abstract class LCIMTypedMessage : LCIMMessage { - protected LCIMTypedMessage() { - - } + 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() { - return new Dictionary { + 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) { @@ -27,6 +48,9 @@ namespace LeanCloud.Realtime { protected virtual void DecodeMessageData(Dictionary msgData) { MessageType = (int)msgData["_lctype"]; + if (msgData.TryGetValue("_lcattrs", out object attrObj)) { + customProperties = LCDecoder.Decode(attrObj) as Dictionary; + } } } } diff --git a/Test/RealtimeConsole/Program.cs b/Test/RealtimeConsole/Program.cs index b0b4dda..c193237 100644 --- a/Test/RealtimeConsole/Program.cs +++ b/Test/RealtimeConsole/Program.cs @@ -57,6 +57,11 @@ namespace RealtimeConsole { world.OnMessageReceived = (conv, 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"); @@ -76,8 +81,11 @@ namespace RealtimeConsole { // Console.WriteLine(member.MemberId); //} - //LCIMTextMessage textMessage = new LCIMTextMessage("hello, world"); - //await conversation.Send(textMessage); + LCIMTextMessage textMessage = new LCIMTextMessage("hello, world"); + textMessage["k1"] = 123; + textMessage["k2"] = "abc"; + textMessage["k3"] = true; + await conversation.Send(textMessage); //LCFile file = new LCFile("avatar", "../../../Storage.Test/assets/hello.png"); //file.MetaData["width"] = 225; @@ -87,9 +95,9 @@ namespace RealtimeConsole { //LCIMImageMessage imageMessage = new LCIMImageMessage(file); //await conversation.Send(imageMessage); - LCGeoPoint location = new LCGeoPoint(11, 12); - LCIMLocationMessage locationMessage = new LCIMLocationMessage(location); - await conversation.Send(locationMessage); + //LCGeoPoint location = new LCGeoPoint(11, 12); + //LCIMLocationMessage locationMessage = new LCIMLocationMessage(location); + //await conversation.Send(locationMessage); } } }