* LCIMChatRoom.cs: chore: 增加会话实例化逻辑
* LCIMClient.cs: * Program.cs: * LCIMPartiallySuccessResult.cs: * LCIMConversation.cs: * LCIMServiceConversation.cs:
parent
87d481f41d
commit
b0b85274ca
|
@ -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<int> GetOnlineMembersCount() {
|
||||
return await GetMembersCount();
|
||||
}
|
||||
|
||||
public async Task<List<string>> 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<string> memberList = response.ConvMessage.M.ToList();
|
||||
return memberList;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<string> 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<string, object> customProperties;
|
||||
|
||||
|
@ -71,7 +68,7 @@ namespace LeanCloud.Realtime {
|
|||
/// 获取对话人数,或暂态对话的在线人数
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<int> Count() {
|
||||
public async Task<int> GetMembersCount() {
|
||||
ConvCommand conv = new ConvCommand {
|
||||
Cid = Id,
|
||||
};
|
||||
|
@ -110,7 +107,7 @@ namespace LeanCloud.Realtime {
|
|||
/// </summary>
|
||||
/// <param name="clientIds">用户 Id</param>
|
||||
/// <returns></returns>
|
||||
public async Task<LCIMConversation> Add(IEnumerable<string> clientIds) {
|
||||
public async Task<LCIMPartiallySuccessResult> Add(IEnumerable<string> 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<string> allowedIds = response.ConvMessage.AllowedPids.ToList();
|
||||
List<ErrorCommand> failedIds = response.ConvMessage.FailedPids.ToList();
|
||||
// TODO 转化为返回
|
||||
|
||||
return this;
|
||||
List<ErrorCommand> errors = response.ConvMessage.FailedPids.ToList();
|
||||
return NewPartiallySuccessResult(allowedIds, errors);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -135,7 +130,7 @@ namespace LeanCloud.Realtime {
|
|||
/// </summary>
|
||||
/// <param name="removeIds">用户 Id</param>
|
||||
/// <returns></returns>
|
||||
public async Task<LCIMConversation> Remove(IEnumerable<string> removeIds) {
|
||||
public async Task<LCIMPartiallySuccessResult> Remove(IEnumerable<string> 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<string> allowedIds = response.ConvMessage.AllowedPids.ToList();
|
||||
List<ErrorCommand> failedIds = response.ConvMessage.FailedPids.ToList();
|
||||
// TODO 转化为返回
|
||||
|
||||
return this;
|
||||
List<ErrorCommand> errors = response.ConvMessage.FailedPids.ToList();
|
||||
return NewPartiallySuccessResult(allowedIds, errors);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -160,7 +153,12 @@ namespace LeanCloud.Realtime {
|
|||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<LCIMConversation> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -168,7 +166,12 @@ namespace LeanCloud.Realtime {
|
|||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<LCIMConversation> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
using System;
|
||||
|
||||
namespace LeanCloud.Realtime {
|
||||
public class LCIMServiceConversation : LCIMConversation {
|
||||
public LCIMServiceConversation(LCIMClient client) : base(client) {
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -14,5 +14,7 @@ namespace LeanCloud.Realtime {
|
|||
|
||||
public LCIMPartiallySuccessResult() {
|
||||
}
|
||||
|
||||
public bool IsSuccess => FailureList == null || FailureList.Count == 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue