diff --git a/Realtime/Conversation/LCIMChatRoom.cs b/Realtime/Conversation/LCIMChatRoom.cs index 352b057..d309edf 100644 --- a/Realtime/Conversation/LCIMChatRoom.cs +++ b/Realtime/Conversation/LCIMChatRoom.cs @@ -1,8 +1,27 @@ -using System; +using System.Linq; +using System.Collections.Generic; +using System.Threading.Tasks; +using LeanCloud.Realtime.Protocol; namespace LeanCloud.Realtime { public class LCIMChatRoom : LCIMConversation { public LCIMChatRoom(LCIMClient client) : base(client) { } + + public async Task GetOnlineMembersCount() { + return await GetMembersCount(); + } + + public async Task> GetOnlineMembers(int limit = 50) { + ConvCommand conv = new ConvCommand { + Cid = Id, + Limit = limit + }; + GenericCommand request = client.NewCommand(CommandType.Conv, OpType.Members); + request.ConvMessage = conv; + GenericCommand response = await client.connection.SendRequest(request); + List memberList = response.ConvMessage.M.ToList(); + return memberList; + } } } diff --git a/Realtime/Conversation/LCIMConversation.cs b/Realtime/Conversation/LCIMConversation.cs index 3eba049..33d4e68 100644 --- a/Realtime/Conversation/LCIMConversation.cs +++ b/Realtime/Conversation/LCIMConversation.cs @@ -6,6 +6,7 @@ using Newtonsoft.Json; using Google.Protobuf; using LeanCloud.Realtime.Protocol; using LeanCloud.Storage.Internal.Codec; +using LeanCloud.Storage; namespace LeanCloud.Realtime { public class LCIMConversation { @@ -26,15 +27,15 @@ namespace LeanCloud.Realtime { } public List MemberIdList { - get; set; + get; internal set; } public DateTime CreatedAt { - get; set; + get; internal set; } public DateTime UpdatedAt { - get; set; + get; internal set; } public DateTime LastMessageAt { @@ -54,11 +55,7 @@ namespace LeanCloud.Realtime { get; private set; } - public virtual bool IsSystem => false; - - public virtual bool IsTransient => false; - - private readonly LCIMClient client; + protected readonly LCIMClient client; private Dictionary customProperties; @@ -71,7 +68,7 @@ namespace LeanCloud.Realtime { /// 获取对话人数,或暂态对话的在线人数 /// /// - public async Task Count() { + public async Task GetMembersCount() { ConvCommand conv = new ConvCommand { Cid = Id, }; @@ -110,7 +107,7 @@ namespace LeanCloud.Realtime { /// /// 用户 Id /// - public async Task Add(IEnumerable clientIds) { + public async Task Add(IEnumerable clientIds) { if (clientIds == null || clientIds.Count() == 0) { throw new ArgumentNullException(nameof(clientIds)); } @@ -124,10 +121,8 @@ namespace LeanCloud.Realtime { request.ConvMessage = conv; GenericCommand response = await client.connection.SendRequest(request); List allowedIds = response.ConvMessage.AllowedPids.ToList(); - List failedIds = response.ConvMessage.FailedPids.ToList(); - // TODO 转化为返回 - - return this; + List errors = response.ConvMessage.FailedPids.ToList(); + return NewPartiallySuccessResult(allowedIds, errors); } /// @@ -135,7 +130,7 @@ namespace LeanCloud.Realtime { /// /// 用户 Id /// - public async Task Remove(IEnumerable removeIds) { + public async Task Remove(IEnumerable removeIds) { if (removeIds == null || removeIds.Count() == 0) { throw new ArgumentNullException(nameof(removeIds)); } @@ -149,10 +144,8 @@ namespace LeanCloud.Realtime { request.ConvMessage = conv; GenericCommand response = await client.connection.SendRequest(request); List allowedIds = response.ConvMessage.AllowedPids.ToList(); - List failedIds = response.ConvMessage.FailedPids.ToList(); - // TODO 转化为返回 - - return this; + List errors = response.ConvMessage.FailedPids.ToList(); + return NewPartiallySuccessResult(allowedIds, errors); } /// @@ -160,7 +153,12 @@ namespace LeanCloud.Realtime { /// /// public async Task Join() { - return await Add(new string[] { client.ClientId }); + LCIMPartiallySuccessResult result = await Add(new string[] { client.ClientId }); + if (!result.IsSuccess) { + LCIMOperationFailure error = result.FailureList[0]; + throw new LCException(error.Code, error.Reason); + } + return this; } /// @@ -168,7 +166,12 @@ namespace LeanCloud.Realtime { /// /// public async Task Quit() { - return await Remove(new string[] { client.ClientId }); + LCIMPartiallySuccessResult result = await Remove(new string[] { client.ClientId }); + if (!result.IsSuccess) { + LCIMOperationFailure error = result.FailureList[0]; + throw new LCException(error.Code, error.Reason); + } + return this; } /// @@ -208,7 +211,7 @@ namespace LeanCloud.Realtime { }; GenericCommand request = client.NewCommand(CommandType.Conv, OpType.Mute); request.ConvMessage = conv; - GenericCommand response = await client.connection.SendRequest(request); + await client.connection.SendRequest(request); IsMute = true; return this; } @@ -223,7 +226,7 @@ namespace LeanCloud.Realtime { }; GenericCommand request = client.NewCommand(CommandType.Conv, OpType.Unmute); request.ConvMessage = conv; - GenericCommand response = await client.connection.SendRequest(request); + await client.connection.SendRequest(request); IsMute = false; return this; } diff --git a/Realtime/Conversation/LCIMServiceConversation.cs b/Realtime/Conversation/LCIMServiceConversation.cs new file mode 100644 index 0000000..403d02b --- /dev/null +++ b/Realtime/Conversation/LCIMServiceConversation.cs @@ -0,0 +1,8 @@ +using System; + +namespace LeanCloud.Realtime { + public class LCIMServiceConversation : LCIMConversation { + public LCIMServiceConversation(LCIMClient client) : base(client) { + } + } +} diff --git a/Realtime/LCIMClient.cs b/Realtime/LCIMClient.cs index 495fdc6..7906c6e 100644 --- a/Realtime/LCIMClient.cs +++ b/Realtime/LCIMClient.cs @@ -4,7 +4,7 @@ using System.Threading.Tasks; using System.Linq; using LeanCloud.Realtime.Internal.WebSocket; using LeanCloud.Realtime.Protocol; -using Google.Protobuf; +using LeanCloud.Storage.Internal.Codec; using Newtonsoft.Json; namespace LeanCloud.Realtime { @@ -143,8 +143,6 @@ namespace LeanCloud.Realtime { ConvCommand conv = new ConvCommand { Transient = transient, Unique = unique, - TempConv = temporary, - TempConvTTL = temporaryTtl }; if (members != null) { conv.M.AddRange(members); @@ -152,16 +150,35 @@ namespace LeanCloud.Realtime { if (!string.IsNullOrEmpty(name)) { conv.N = name; } + if (temporary) { + conv.TempConv = temporary; + conv.TempConvTTL = temporaryTtl; + } if (properties != null) { conv.Attr = new JsonObjectMessage { - Data = JsonConvert.SerializeObject(properties) + Data = JsonConvert.SerializeObject(LCEncoder.Encode(properties)) }; } command.ConvMessage = conv; GenericCommand response = await connection.SendRequest(command); - LCIMConversation conversation = GetOrCreateConversation(response.ConvMessage.Cid); + string convId = response.ConvMessage.Cid; + if (!conversationDict.TryGetValue(convId, out LCIMConversation conversation)) { + if (transient) { + conversation = new LCIMChatRoom(this); + } else if (temporary) { + conversation = new LCIMTemporaryConversation(this); + } else if (properties != null && properties.ContainsKey("system")) { + conversation = new LCIMServiceConversation(this); + } else { + conversation = new LCIMConversation(this); + } + conversationDict[convId] = conversation; + } + // 合并请求数据 + conversation.Name = name; + conversation.MemberIdList = members?.ToList(); + // 合并服务端推送的数据 conversation.MergeFrom(response.ConvMessage); - conversationDict[conversation.Id] = conversation; return conversation; } diff --git a/Realtime/LCIMPartiallySuccessResult.cs b/Realtime/LCIMPartiallySuccessResult.cs index d0d416c..d9e8247 100644 --- a/Realtime/LCIMPartiallySuccessResult.cs +++ b/Realtime/LCIMPartiallySuccessResult.cs @@ -14,5 +14,7 @@ namespace LeanCloud.Realtime { public LCIMPartiallySuccessResult() { } + + public bool IsSuccess => FailureList == null || FailureList.Count == 0; } } diff --git a/Test/RealtimeConsole/Program.cs b/Test/RealtimeConsole/Program.cs index c193237..805b427 100644 --- a/Test/RealtimeConsole/Program.cs +++ b/Test/RealtimeConsole/Program.cs @@ -12,12 +12,6 @@ namespace RealtimeConsole { public static void Main(string[] args) { Console.WriteLine("Hello World!"); - _ = Start(); - - Console.ReadKey(true); - } - - static async Task Start() { LCLogger.LogDelegate += (level, info) => { switch (level) { case LCLogLevel.Debug: @@ -36,6 +30,38 @@ namespace RealtimeConsole { }; LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com"); + //_ = Start(); + + //_ = ChatRoom(); + + _ = TemporaryConversation(); + + Console.ReadKey(true); + } + + static async Task ChatRoom() { + LCIMClient hello = new LCIMClient("hello"); + await hello.Open(); + + string name = Guid.NewGuid().ToString(); + LCIMChatRoom chatRoom = await hello.CreateChatRoom(name); + Console.WriteLine(chatRoom.Name); + } + + static async Task TemporaryConversation() { + string c1Id = Guid.NewGuid().ToString(); + LCIMClient c1 = new LCIMClient(c1Id); + await c1.Open(); + + string c2Id = Guid.NewGuid().ToString(); + LCIMClient c2 = new LCIMClient(c2Id); + await c2.Open(); + + LCIMTemporaryConversation temporaryConversation = await c1.CreateTemporaryConversation(new string[] { c2Id }); + Console.WriteLine(temporaryConversation.Id); + } + + static async Task Start() { LCIMClient hello = new LCIMClient("hello"); await hello.Open();