From bfd14f5b59ea4aef9ee002ba4046776ac8368324 Mon Sep 17 00:00:00 2001 From: oneRain Date: Thu, 26 Mar 2020 16:08:35 +0800 Subject: [PATCH] * LCIMClient.cs: * Realtime.csproj: * Program.cs: * LCIMMessage.cs: * Conversation.cs: * LCIMChatRoom.cs: * LCRTMRouter.cs: * LCIMConversation.cs: * LCIMConversationQuery.cs: * LCIMController.cs: * LCIMGoAwayController.cs: * LCIMUnreadController.cs: * LCWebSocketConnection.cs: * LCIMMessageController.cs: * LCIMSessionController.cs: * LCIMConversationController.cs: * LCIMTypedMessage.cs: refactor --- Realtime/Conversation/LCIMConversation.cs | 4 + .../Conversation/LCIMConversationQuery.cs | 80 +++++--------- .../Controller/LCIMConversationController.cs | 104 ++++++++++++++---- .../Controller/LCIMGoAwayController.cs | 2 +- .../Controller/LCIMMessageController.cs | 14 ++- .../Controller/LCIMUnreadController.cs | 54 +++++++-- Realtime/LCIMClient.cs | 35 ++++-- Realtime/Message/LCIMMessage.cs | 2 +- Realtime/Message/LCIMTypedMessage.cs | 2 +- Test/RealtimeConsole/Program.cs | 55 ++++++--- 10 files changed, 231 insertions(+), 121 deletions(-) diff --git a/Realtime/Conversation/LCIMConversation.cs b/Realtime/Conversation/LCIMConversation.cs index 28b4a27..c62d624 100644 --- a/Realtime/Conversation/LCIMConversation.cs +++ b/Realtime/Conversation/LCIMConversation.cs @@ -324,6 +324,10 @@ namespace LeanCloud.Realtime { return await Client.MessageController.QueryMessages(Id, start, end, direction, limit, messageType); } + internal static bool IsTemporayConversation(string convId) { + return convId.StartsWith("_tmp:"); + } + internal void MergeFrom(ConvCommand conv) { if (conv.HasCid) { Id = conv.Cid; diff --git a/Realtime/Conversation/LCIMConversationQuery.cs b/Realtime/Conversation/LCIMConversationQuery.cs index 887caf4..e295d10 100644 --- a/Realtime/Conversation/LCIMConversationQuery.cs +++ b/Realtime/Conversation/LCIMConversationQuery.cs @@ -2,19 +2,17 @@ using System.Collections; using System.Collections.Generic; using LeanCloud.Storage.Internal.Query; -using LeanCloud.Realtime.Protocol; -using LeanCloud.Storage.Internal; -using LeanCloud.Storage.Internal.Codec; -using Newtonsoft.Json; namespace LeanCloud.Realtime { public class LCIMConversationQuery { - private LCCompositionalCondition condition; + internal LCCompositionalCondition Condition { + get; private set; + } private LCIMClient client; public LCIMConversationQuery(LCIMClient client) { - condition = new LCCompositionalCondition(); + Condition = new LCCompositionalCondition(); this.client = client; } @@ -25,7 +23,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery WhereEqualTo(string key, object value) { - condition.WhereEqualTo(key, value); + Condition.WhereEqualTo(key, value); return this; } @@ -36,7 +34,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery WhereNotEqualTo(string key, object value) { - condition.WhereNotEqualTo(key, value); + Condition.WhereNotEqualTo(key, value); return this; } @@ -47,7 +45,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery WhereContainedIn(string key, IEnumerable values) { - condition.WhereContainedIn(key, values); + Condition.WhereContainedIn(key, values); return this; } @@ -58,7 +56,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery WhereContainsAll(string key, IEnumerable values) { - condition.WhereContainsAll(key, values); + Condition.WhereContainsAll(key, values); return this; } @@ -68,7 +66,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery WhereExists(string key) { - condition.WhereExists(key); + Condition.WhereExists(key); return this; } @@ -78,7 +76,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery WhereDoesNotExist(string key) { - condition.WhereDoesNotExist(key); + Condition.WhereDoesNotExist(key); return this; } @@ -89,7 +87,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery WhereSizeEqualTo(string key, int size) { - condition.WhereSizeEqualTo(key, size); + Condition.WhereSizeEqualTo(key, size); return this; } @@ -100,7 +98,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery WhereGreaterThan(string key, object value) { - condition.WhereGreaterThan(key, value); + Condition.WhereGreaterThan(key, value); return this; } @@ -111,7 +109,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery WhereGreaterThanOrEqualTo(string key, object value) { - condition.WhereGreaterThanOrEqualTo(key, value); + Condition.WhereGreaterThanOrEqualTo(key, value); return this; } @@ -122,7 +120,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery WhereLessThan(string key, object value) { - condition.WhereLessThan(key, value); + Condition.WhereLessThan(key, value); return this; } @@ -133,7 +131,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery WhereLessThanOrEqualTo(string key, object value) { - condition.WhereLessThanOrEqualTo(key, value); + Condition.WhereLessThanOrEqualTo(key, value); return this; } @@ -144,7 +142,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery WhereStartsWith(string key, string prefix) { - condition.WhereStartsWith(key, prefix); + Condition.WhereStartsWith(key, prefix); return this; } @@ -155,7 +153,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery WhereEndsWith(string key, string suffix) { - condition.WhereEndsWith(key, suffix); + Condition.WhereEndsWith(key, suffix); return this; } @@ -166,7 +164,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery WhereContains(string key, string subString) { - condition.WhereContains(key, subString); + Condition.WhereContains(key, subString); return this; } @@ -176,7 +174,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery OrderBy(string key) { - condition.OrderBy(key); + Condition.OrderBy(key); return this; } @@ -186,7 +184,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery OrderByDescending(string key) { - condition.OrderByDescending(key); + Condition.OrderByDescending(key); return this; } @@ -196,7 +194,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery Include(string key) { - condition.Include(key); + Condition.Include(key); return this; } @@ -206,7 +204,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery Select(string key) { - condition.Select(key); + Condition.Select(key); return this; } @@ -216,7 +214,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery Skip(int value) { - condition.Skip = value; + Condition.Skip = value; return this; } @@ -226,7 +224,7 @@ namespace LeanCloud.Realtime { /// /// public LCIMConversationQuery Limit(int value) { - condition.Limit = value; + Condition.Limit = value; return this; } @@ -239,35 +237,7 @@ namespace LeanCloud.Realtime { /// /// public async Task> Find() { - GenericCommand command = new GenericCommand { - Cmd = CommandType.Conv, - Op = OpType.Query, - AppId = LCApplication.AppId, - PeerId = client.Id, - }; - ConvCommand conv = new ConvCommand(); - string where = condition.BuildWhere(); - if (!string.IsNullOrEmpty(where)) { - conv.Where = new JsonObjectMessage { - Data = where - }; - } - command.ConvMessage = conv; - GenericCommand response = await client.Connection.SendRequest(command); - JsonObjectMessage results = response.ConvMessage.Results; - List convs = JsonConvert.DeserializeObject>(results.Data, new LCJsonConverter()); - List convList = new List(convs.Count); - foreach (object c in convs) { - Dictionary cd = c as Dictionary; - string convId = cd["objectId"] as string; - if (!client.ConversationDict.TryGetValue(convId, out LCIMConversation conversation)) { - conversation = new LCIMConversation(client); - client.ConversationDict[convId] = conversation; - } - conversation.MergeFrom(cd); - convList.Add(conversation); - } - return convList; + return await client.ConversationController.Find(this); } } } diff --git a/Realtime/Internal/Controller/LCIMConversationController.cs b/Realtime/Internal/Controller/LCIMConversationController.cs index 4fb0350..8cfd539 100644 --- a/Realtime/Internal/Controller/LCIMConversationController.cs +++ b/Realtime/Internal/Controller/LCIMConversationController.cs @@ -6,6 +6,7 @@ using Newtonsoft.Json; using LeanCloud.Realtime.Protocol; using LeanCloud.Storage.Internal; using LeanCloud.Storage.Internal.Codec; +using LeanCloud.Common; using Google.Protobuf; namespace LeanCloud.Realtime.Internal.Controller { @@ -14,17 +15,6 @@ namespace LeanCloud.Realtime.Internal.Controller { } - /// - /// 创建对话 - /// - /// - /// - /// - /// - /// - /// - /// - /// internal async Task CreateConv( IEnumerable members = null, string name = null, @@ -92,7 +82,8 @@ namespace LeanCloud.Realtime.Internal.Controller { return response.ConvMessage.Count; } - internal async Task Read(string convId, LCIMMessage message) { + internal async Task Read(string convId, + LCIMMessage message) { ReadCommand read = new ReadCommand(); ReadTuple tuple = new ReadTuple { Cid = convId, @@ -105,7 +96,8 @@ namespace LeanCloud.Realtime.Internal.Controller { await Client.Connection.SendRequest(request); } - internal async Task> UpdateInfo(string convId, Dictionary attributes) { + internal async Task> UpdateInfo(string convId, + Dictionary attributes) { ConvCommand conv = new ConvCommand { Cid = convId, }; @@ -124,7 +116,8 @@ namespace LeanCloud.Realtime.Internal.Controller { return null; } - internal async Task AddMembers(string convId, IEnumerable clientIds) { + internal async Task AddMembers(string convId, + IEnumerable clientIds) { ConvCommand conv = new ConvCommand { Cid = convId, }; @@ -147,7 +140,8 @@ namespace LeanCloud.Realtime.Internal.Controller { return NewPartiallySuccessResult(allowedIds, errors); } - internal async Task RemoveMembers(string convId, IEnumerable removeIds) { + internal async Task RemoveMembers(string convId, + IEnumerable removeIds) { ConvCommand conv = new ConvCommand { Cid = convId, }; @@ -188,7 +182,8 @@ namespace LeanCloud.Realtime.Internal.Controller { await Client.Connection.SendRequest(request); } - internal async Task MuteMembers(string convId, IEnumerable clientIds) { + internal async Task MuteMembers(string convId, + IEnumerable clientIds) { if (clientIds == null || clientIds.Count() == 0) { throw new ArgumentNullException(nameof(clientIds)); } @@ -202,7 +197,8 @@ namespace LeanCloud.Realtime.Internal.Controller { return NewPartiallySuccessResult(response.ConvMessage.AllowedPids, response.ConvMessage.FailedPids); } - internal async Task UnmuteMembers(string convId, IEnumerable clientIds) { + internal async Task UnmuteMembers(string convId, + IEnumerable clientIds) { ConvCommand conv = new ConvCommand { Cid = convId }; @@ -213,7 +209,8 @@ namespace LeanCloud.Realtime.Internal.Controller { return NewPartiallySuccessResult(response.ConvMessage.AllowedPids, response.ConvMessage.FailedPids); } - internal async Task BlockMembers(string convId, IEnumerable clientIds) { + internal async Task BlockMembers(string convId, + IEnumerable clientIds) { BlacklistCommand blacklist = new BlacklistCommand { SrcCid = convId, }; @@ -233,7 +230,8 @@ namespace LeanCloud.Realtime.Internal.Controller { return NewPartiallySuccessResult(response.BlacklistMessage.AllowedPids, response.BlacklistMessage.FailedPids); } - internal async Task UnblockMembers(string convId, IEnumerable clientIds) { + internal async Task UnblockMembers(string convId, + IEnumerable clientIds) { BlacklistCommand blacklist = new BlacklistCommand { SrcCid = convId, }; @@ -253,7 +251,9 @@ namespace LeanCloud.Realtime.Internal.Controller { return NewPartiallySuccessResult(response.BlacklistMessage.AllowedPids, response.BlacklistMessage.FailedPids); } - internal async Task UpdateMemberRole(string convId, string memberId, string role) { + internal async Task UpdateMemberRole(string convId, + string memberId, + string role) { ConvCommand conv = new ConvCommand { Cid = convId, TargetClientId = memberId, @@ -292,7 +292,9 @@ namespace LeanCloud.Realtime.Internal.Controller { return memberList; } - internal async Task QueryMutedMembers(string convId, int limit = 10, string next = null) { + internal async Task QueryMutedMembers(string convId, + int limit = 10, + string next = null) { ConvCommand conv = new ConvCommand { Cid = convId, Limit = limit, @@ -307,7 +309,9 @@ namespace LeanCloud.Realtime.Internal.Controller { }; } - internal async Task QueryBlockedMembers(string convId, int limit = 10, string next = null) { + internal async Task QueryBlockedMembers(string convId, + int limit = 10, + string next = null) { BlacklistCommand black = new BlacklistCommand { SrcCid = convId, Limit = limit, @@ -322,7 +326,61 @@ namespace LeanCloud.Realtime.Internal.Controller { }; } - private LCIMPartiallySuccessResult NewPartiallySuccessResult(IEnumerable succesfulIds, IEnumerable errors) { + internal async Task> Find(LCIMConversationQuery query) { + GenericCommand command = new GenericCommand { + Cmd = CommandType.Conv, + Op = OpType.Query, + AppId = LCApplication.AppId, + PeerId = Client.Id, + }; + ConvCommand convMessage = new ConvCommand(); + string where = query.Condition.BuildWhere(); + if (!string.IsNullOrEmpty(where)) { + try { + convMessage.Where = new JsonObjectMessage { + Data = where + }; + } catch (Exception e) { + LCLogger.Error(e.Message); + } + } + command.ConvMessage = convMessage; + GenericCommand response = await Connection.SendRequest(command); + JsonObjectMessage results = response.ConvMessage.Results; + List convs = JsonConvert.DeserializeObject>(results.Data, new LCJsonConverter()); + List convList = convs.Select(item => { + Dictionary conv = item as Dictionary; + string convId = conv["objectId"] as string; + if (!Client.ConversationDict.TryGetValue(convId, out LCIMConversation conversation)) { + // TODO 解析是哪种类型的对话 + + conversation = new LCIMConversation(Client); + Client.ConversationDict[convId] = conversation; + } + conversation.MergeFrom(conv); + return conversation; + }).ToList(); + return convList; + } + + internal async Task> GetTemporaryConversations(IEnumerable convIds) { + ConvCommand convMessage = new ConvCommand(); + convMessage.TempConvIds.AddRange(convIds); + GenericCommand request = Client.NewCommand(CommandType.Conv, OpType.Query); + request.ConvMessage = convMessage; + GenericCommand response = await Connection.SendRequest(request); + JsonObjectMessage results = response.ConvMessage.Results; + List convs = JsonConvert.DeserializeObject>(results.Data, new LCJsonConverter()); + List convList = convs.Select(item => { + LCIMTemporaryConversation temporaryConversation = new LCIMTemporaryConversation(Client); + temporaryConversation.MergeFrom(item as Dictionary); + return temporaryConversation; + }).ToList(); + return convList; + } + + private LCIMPartiallySuccessResult NewPartiallySuccessResult(IEnumerable succesfulIds, + IEnumerable errors) { LCIMPartiallySuccessResult result = new LCIMPartiallySuccessResult { SuccessfulClientIdList = succesfulIds.ToList() }; diff --git a/Realtime/Internal/Controller/LCIMGoAwayController.cs b/Realtime/Internal/Controller/LCIMGoAwayController.cs index 1756c54..3619e98 100644 --- a/Realtime/Internal/Controller/LCIMGoAwayController.cs +++ b/Realtime/Internal/Controller/LCIMGoAwayController.cs @@ -8,7 +8,7 @@ namespace LeanCloud.Realtime.Internal.Controller { } internal override async Task OnNotification(GenericCommand notification) { - // TODO 清空缓存,断开连接,等待重新连接 + // 清空缓存,断开连接,等待重新连接 Connection.Router.Reset(); await Connection.Close(); } diff --git a/Realtime/Internal/Controller/LCIMMessageController.cs b/Realtime/Internal/Controller/LCIMMessageController.cs index 407ed5e..d9134e8 100644 --- a/Realtime/Internal/Controller/LCIMMessageController.cs +++ b/Realtime/Internal/Controller/LCIMMessageController.cs @@ -12,7 +12,8 @@ namespace LeanCloud.Realtime.Internal.Controller { } - internal async Task Send(string convId, LCIMMessage message) { + internal async Task Send(string convId, + LCIMMessage message) { DirectCommand direct = new DirectCommand { FromPeerId = Client.Id, Cid = convId, @@ -34,7 +35,8 @@ namespace LeanCloud.Realtime.Internal.Controller { return message; } - internal async Task RecallMessage(string convId, LCIMMessage message) { + internal async Task RecallMessage(string convId, + LCIMMessage message) { PatchCommand patch = new PatchCommand(); PatchItem item = new PatchItem { Cid = convId, @@ -47,7 +49,9 @@ namespace LeanCloud.Realtime.Internal.Controller { await Client.Connection.SendRequest(request); } - internal async Task UpdateMessage(string convId, LCIMMessage oldMessage, LCIMMessage newMessage) { + internal async Task UpdateMessage(string convId, + LCIMMessage oldMessage, + LCIMMessage newMessage) { PatchCommand patch = new PatchCommand(); PatchItem item = new PatchItem { Cid = convId, @@ -115,9 +119,9 @@ namespace LeanCloud.Realtime.Internal.Controller { } else { // 文本消息 string messageData = direct.Msg; - Dictionary msg = JsonConvert.DeserializeObject>(messageData, + Dictionary msgData = JsonConvert.DeserializeObject>(messageData, new LCJsonConverter()); - int msgType = (int)(long)msg["_lctype"]; + int msgType = (int)msgData["_lctype"]; switch (msgType) { case -1: message = new LCIMTextMessage(); diff --git a/Realtime/Internal/Controller/LCIMUnreadController.cs b/Realtime/Internal/Controller/LCIMUnreadController.cs index 08c4fb2..7ffdcb5 100644 --- a/Realtime/Internal/Controller/LCIMUnreadController.cs +++ b/Realtime/Internal/Controller/LCIMUnreadController.cs @@ -1,7 +1,9 @@ -using System.Threading.Tasks; +using System.Linq; +using System.Threading.Tasks; using System.Collections.Generic; using Newtonsoft.Json; using LeanCloud.Realtime.Protocol; +using LeanCloud.Storage.Internal; namespace LeanCloud.Realtime.Internal.Controller { internal class LCIMUnreadController : LCIMController { @@ -10,16 +12,48 @@ namespace LeanCloud.Realtime.Internal.Controller { internal override async Task OnNotification(GenericCommand notification) { UnreadCommand unread = notification.UnreadMessage; - List conversationList = new List(); - foreach (UnreadTuple conv in unread.Convs) { - // 查询对话 - LCIMConversation conversation = await Client.GetOrQueryConversation(conv.Cid); + + IEnumerable convIds = unread.Convs + .Select(conv => conv.Cid); + Dictionary conversations = (await Client.GetConversationList(convIds)) + .ToDictionary(item => item.Id); + List conversationList = unread.Convs.Select(conv => { + LCIMConversation conversation = conversations[conv.Cid]; conversation.Unread = conv.Unread; - // TODO 反序列化对话 - // 最后一条消息 - JsonConvert.DeserializeObject>(conv.Data); - conversationList.Add(conversation); - } + // 解析最后一条消息 + Dictionary msgData = JsonConvert.DeserializeObject>(conv.Data, + new LCJsonConverter()); + int msgType = (int)msgData["_lctype"]; + LCIMMessage message = null; + switch (msgType) { + case -1: + message = new LCIMTextMessage(); + break; + case -2: + message = new LCIMImageMessage(); + break; + case -3: + message = new LCIMAudioMessage(); + break; + case -4: + message = new LCIMVideoMessage(); + break; + case -5: + message = new LCIMLocationMessage(); + break; + case -6: + message = new LCIMFileMessage(); + break; + default: + break; + } + message.ConversationId = conv.Cid; + message.Id = conv.Mid; + message.FromClientId = conv.From; + message.SentTimestamp = conv.Timestamp; + conversation.LastMessage = message; + return conversation; + }).ToList(); Client.OnUnreadMessagesCountUpdated?.Invoke(conversationList); } } diff --git a/Realtime/LCIMClient.cs b/Realtime/LCIMClient.cs index dd41a22..2f84a84 100644 --- a/Realtime/LCIMClient.cs +++ b/Realtime/LCIMClient.cs @@ -300,7 +300,7 @@ namespace LeanCloud.Realtime { } /// - /// 获取某个特定的对话 + /// 根据 id 获取对话 /// /// /// @@ -308,10 +308,17 @@ namespace LeanCloud.Realtime { if (string.IsNullOrEmpty(id)) { throw new ArgumentNullException(nameof(id)); } + if (LCIMConversation.IsTemporayConversation(id)) { + List temporaryConversationList = await ConversationController.GetTemporaryConversations(new string[] { id }); + if (temporaryConversationList == null || temporaryConversationList.Count < 1) { + return null; + } + return temporaryConversationList[0]; + } LCIMConversationQuery query = GetQuery() .WhereEqualTo("objectId", id) .Limit(1); - List results = await query.Find(); + List results = await ConversationController.Find(query); if (results == null || results.Count < 1) { return null; } @@ -327,12 +334,20 @@ namespace LeanCloud.Realtime { if (ids == null || ids.Count() == 0) { throw new ArgumentNullException(nameof(ids)); } - List conversationList = new List(); - foreach (string id in ids) { - LCIMConversation conversation = await GetConversation(id); - conversationList.Add(conversation); - } - return conversationList; + // 区分临时对话 + IEnumerable tempConvIds = ids.Where(item => { + return LCIMConversation.IsTemporayConversation(item); + }); + IEnumerable convIds = ids.Where(item => { + return !tempConvIds.Contains(item); + }); + List temporaryConversations = await ConversationController.GetTemporaryConversations(tempConvIds); + LCIMConversationQuery query = GetQuery() + .WhereContainedIn("objectId", convIds) + .Limit(999); + List conversations = await ConversationController.Find(query); + conversations.AddRange(temporaryConversations); + return conversations; } /// @@ -343,8 +358,6 @@ namespace LeanCloud.Realtime { return new LCIMConversationQuery(this); } - #region 通知处理 - private async Task OnNotification(GenericCommand notification) { switch (notification.Cmd) { case CommandType.Session: @@ -367,8 +380,6 @@ namespace LeanCloud.Realtime { } } - #endregion - internal async Task GetOrQueryConversation(string convId) { if (ConversationDict.TryGetValue(convId, out LCIMConversation conversation)) { return conversation; diff --git a/Realtime/Message/LCIMMessage.cs b/Realtime/Message/LCIMMessage.cs index 7805eba..1edd163 100644 --- a/Realtime/Message/LCIMMessage.cs +++ b/Realtime/Message/LCIMMessage.cs @@ -82,7 +82,7 @@ namespace LeanCloud.Realtime { ConversationId = direct.Cid; Id = direct.Id; FromClientId = direct.FromPeerId; - DeliveredTimestamp = direct.Timestamp; + SentTimestamp = direct.Timestamp; } } } diff --git a/Realtime/Message/LCIMTypedMessage.cs b/Realtime/Message/LCIMTypedMessage.cs index 1e598a4..7152dd7 100644 --- a/Realtime/Message/LCIMTypedMessage.cs +++ b/Realtime/Message/LCIMTypedMessage.cs @@ -55,7 +55,7 @@ namespace LeanCloud.Realtime { internal static LCIMTypedMessage Deserialize(Dictionary messageData) { LCIMTypedMessage message = null; - int msgType = (int)(long)messageData["_lctype"]; + int msgType = (int)messageData["_lctype"]; switch (msgType) { case -1: message = new LCIMTextMessage(); diff --git a/Test/RealtimeConsole/Program.cs b/Test/RealtimeConsole/Program.cs index 8e24872..3e4d373 100644 --- a/Test/RealtimeConsole/Program.cs +++ b/Test/RealtimeConsole/Program.cs @@ -4,13 +4,12 @@ using System.Threading; using System.Threading.Tasks; using LeanCloud; using LeanCloud.Common; -using LeanCloud.Storage; using LeanCloud.Realtime; namespace RealtimeConsole { class MainClass { public static void Main(string[] args) { - Console.WriteLine("Hello World!"); + Console.WriteLine($"Hello World at {Thread.CurrentThread.ManagedThreadId}"); LCLogger.LogDelegate += (level, info) => { switch (level) { @@ -30,12 +29,18 @@ namespace RealtimeConsole { }; LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com"); + SingleThreadSynchronizationContext.Run(async () => { + Console.WriteLine($"start at {Thread.CurrentThread.ManagedThreadId}"); + await Run("cc2"); + //await ChatRoom(); + //await TemporaryConversation(); + //await CreateConversation(); + //await QueryMyConversation(); + Console.WriteLine($"done at {Thread.CurrentThread.ManagedThreadId}"); + }); + //Conversation().Wait(); - //_ = ChatRoom(); - - //_ = TemporaryConversation(); - //_ = Signature(); //_ = Block(); @@ -46,19 +51,38 @@ namespace RealtimeConsole { //_ = OpenAndClose(); - SendMessage().Wait(); + //SendMessage().Wait(); //Unread().Wait(); Console.ReadKey(true); } - static async Task Run(int s) { - for (int i = 0; i < s; i++) { - Console.WriteLine($"run {i}"); - await Task.Delay(1000); - Console.WriteLine($"run {i} done"); - } + static async Task Run(string id) { + LCIMClient client = new LCIMClient(id); + await client.Open(); + client.OnMessage = (conversation, message) => { + Console.WriteLine($"recv: {conversation.Id}, {message.Id} at {Thread.CurrentThread.ManagedThreadId}"); + }; + } + + static async Task CreateConversation() { + LCIMClient cc1 = new LCIMClient("cc1"); + await cc1.Open(); + //await cc1.CreateChatRoom("leancloud chat"); + await cc1.CreateTemporaryConversation(new string[] { "cc2", "cc3" }); + //await cc1.CreateConversation(new string[] { "cc4" }); + } + + static async Task QueryMyConversation() { + LCIMClient cc1 = new LCIMClient("cc1"); + await cc1.Open(); + List conversationList = await cc1.GetQuery() + .WhereEqualTo("objectId", "5e7c283790aef5aa846b5683") + .Find(); + conversationList.ForEach(conv => { + Console.WriteLine($"convId: {conv.Id}"); + }); } static async Task Unread() { @@ -72,14 +96,19 @@ namespace RealtimeConsole { } static async Task SendMessage() { + Console.WriteLine($"start at {Thread.CurrentThread.ManagedThreadId}"); try { LCIMClient u1 = new LCIMClient("u1"); await u1.Open(); LCIMConversation conversation = await u1.CreateConversation(new string[] { "u2" }); + Console.WriteLine($"open at {Thread.CurrentThread.ManagedThreadId}"); + LCIMTextMessage textMessage = new LCIMTextMessage("hello, text message"); await conversation.Send(textMessage); + Console.WriteLine($"send at {Thread.CurrentThread.ManagedThreadId}"); + //LCFile file = new LCFile("avatar", "../../../Storage.Test/assets/hello.png"); //await file.Save(); //LCIMImageMessage imageMessage = new LCIMImageMessage(file);