* 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
oneRain 2020-03-26 16:08:35 +08:00
parent 890fdc76f4
commit bfd14f5b59
10 changed files with 231 additions and 121 deletions

View File

@ -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;

View File

@ -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 {
/// <param name="value"></param>
/// <returns></returns>
public LCIMConversationQuery WhereEqualTo(string key, object value) {
condition.WhereEqualTo(key, value);
Condition.WhereEqualTo(key, value);
return this;
}
@ -36,7 +34,7 @@ namespace LeanCloud.Realtime {
/// <param name="value"></param>
/// <returns></returns>
public LCIMConversationQuery WhereNotEqualTo(string key, object value) {
condition.WhereNotEqualTo(key, value);
Condition.WhereNotEqualTo(key, value);
return this;
}
@ -47,7 +45,7 @@ namespace LeanCloud.Realtime {
/// <param name="values"></param>
/// <returns></returns>
public LCIMConversationQuery WhereContainedIn(string key, IEnumerable values) {
condition.WhereContainedIn(key, values);
Condition.WhereContainedIn(key, values);
return this;
}
@ -58,7 +56,7 @@ namespace LeanCloud.Realtime {
/// <param name="values"></param>
/// <returns></returns>
public LCIMConversationQuery WhereContainsAll(string key, IEnumerable values) {
condition.WhereContainsAll(key, values);
Condition.WhereContainsAll(key, values);
return this;
}
@ -68,7 +66,7 @@ namespace LeanCloud.Realtime {
/// <param name="key"></param>
/// <returns></returns>
public LCIMConversationQuery WhereExists(string key) {
condition.WhereExists(key);
Condition.WhereExists(key);
return this;
}
@ -78,7 +76,7 @@ namespace LeanCloud.Realtime {
/// <param name="key"></param>
/// <returns></returns>
public LCIMConversationQuery WhereDoesNotExist(string key) {
condition.WhereDoesNotExist(key);
Condition.WhereDoesNotExist(key);
return this;
}
@ -89,7 +87,7 @@ namespace LeanCloud.Realtime {
/// <param name="size"></param>
/// <returns></returns>
public LCIMConversationQuery WhereSizeEqualTo(string key, int size) {
condition.WhereSizeEqualTo(key, size);
Condition.WhereSizeEqualTo(key, size);
return this;
}
@ -100,7 +98,7 @@ namespace LeanCloud.Realtime {
/// <param name="value"></param>
/// <returns></returns>
public LCIMConversationQuery WhereGreaterThan(string key, object value) {
condition.WhereGreaterThan(key, value);
Condition.WhereGreaterThan(key, value);
return this;
}
@ -111,7 +109,7 @@ namespace LeanCloud.Realtime {
/// <param name="value"></param>
/// <returns></returns>
public LCIMConversationQuery WhereGreaterThanOrEqualTo(string key, object value) {
condition.WhereGreaterThanOrEqualTo(key, value);
Condition.WhereGreaterThanOrEqualTo(key, value);
return this;
}
@ -122,7 +120,7 @@ namespace LeanCloud.Realtime {
/// <param name="value"></param>
/// <returns></returns>
public LCIMConversationQuery WhereLessThan(string key, object value) {
condition.WhereLessThan(key, value);
Condition.WhereLessThan(key, value);
return this;
}
@ -133,7 +131,7 @@ namespace LeanCloud.Realtime {
/// <param name="value"></param>
/// <returns></returns>
public LCIMConversationQuery WhereLessThanOrEqualTo(string key, object value) {
condition.WhereLessThanOrEqualTo(key, value);
Condition.WhereLessThanOrEqualTo(key, value);
return this;
}
@ -144,7 +142,7 @@ namespace LeanCloud.Realtime {
/// <param name="prefix"></param>
/// <returns></returns>
public LCIMConversationQuery WhereStartsWith(string key, string prefix) {
condition.WhereStartsWith(key, prefix);
Condition.WhereStartsWith(key, prefix);
return this;
}
@ -155,7 +153,7 @@ namespace LeanCloud.Realtime {
/// <param name="suffix"></param>
/// <returns></returns>
public LCIMConversationQuery WhereEndsWith(string key, string suffix) {
condition.WhereEndsWith(key, suffix);
Condition.WhereEndsWith(key, suffix);
return this;
}
@ -166,7 +164,7 @@ namespace LeanCloud.Realtime {
/// <param name="subString"></param>
/// <returns></returns>
public LCIMConversationQuery WhereContains(string key, string subString) {
condition.WhereContains(key, subString);
Condition.WhereContains(key, subString);
return this;
}
@ -176,7 +174,7 @@ namespace LeanCloud.Realtime {
/// <param name="key"></param>
/// <returns></returns>
public LCIMConversationQuery OrderBy(string key) {
condition.OrderBy(key);
Condition.OrderBy(key);
return this;
}
@ -186,7 +184,7 @@ namespace LeanCloud.Realtime {
/// <param name="key"></param>
/// <returns></returns>
public LCIMConversationQuery OrderByDescending(string key) {
condition.OrderByDescending(key);
Condition.OrderByDescending(key);
return this;
}
@ -196,7 +194,7 @@ namespace LeanCloud.Realtime {
/// <param name="key"></param>
/// <returns></returns>
public LCIMConversationQuery Include(string key) {
condition.Include(key);
Condition.Include(key);
return this;
}
@ -206,7 +204,7 @@ namespace LeanCloud.Realtime {
/// <param name="key"></param>
/// <returns></returns>
public LCIMConversationQuery Select(string key) {
condition.Select(key);
Condition.Select(key);
return this;
}
@ -216,7 +214,7 @@ namespace LeanCloud.Realtime {
/// <param name="value"></param>
/// <returns></returns>
public LCIMConversationQuery Skip(int value) {
condition.Skip = value;
Condition.Skip = value;
return this;
}
@ -226,7 +224,7 @@ namespace LeanCloud.Realtime {
/// <param name="value"></param>
/// <returns></returns>
public LCIMConversationQuery Limit(int value) {
condition.Limit = value;
Condition.Limit = value;
return this;
}
@ -239,35 +237,7 @@ namespace LeanCloud.Realtime {
/// </summary>
/// <returns></returns>
public async Task<List<LCIMConversation>> 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<object> convs = JsonConvert.DeserializeObject<List<object>>(results.Data, new LCJsonConverter());
List<LCIMConversation> convList = new List<LCIMConversation>(convs.Count);
foreach (object c in convs) {
Dictionary<string, object> cd = c as Dictionary<string, object>;
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);
}
}
}

View File

@ -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 {
}
/// <summary>
/// 创建对话
/// </summary>
/// <param name="members"></param>
/// <param name="name"></param>
/// <param name="transient"></param>
/// <param name="unique"></param>
/// <param name="temporary"></param>
/// <param name="temporaryTtl"></param>
/// <param name="properties"></param>
/// <returns></returns>
internal async Task<LCIMConversation> CreateConv(
IEnumerable<string> 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<Dictionary<string, object>> UpdateInfo(string convId, Dictionary<string, object> attributes) {
internal async Task<Dictionary<string, object>> UpdateInfo(string convId,
Dictionary<string, object> attributes) {
ConvCommand conv = new ConvCommand {
Cid = convId,
};
@ -124,7 +116,8 @@ namespace LeanCloud.Realtime.Internal.Controller {
return null;
}
internal async Task<LCIMPartiallySuccessResult> AddMembers(string convId, IEnumerable<string> clientIds) {
internal async Task<LCIMPartiallySuccessResult> AddMembers(string convId,
IEnumerable<string> clientIds) {
ConvCommand conv = new ConvCommand {
Cid = convId,
};
@ -147,7 +140,8 @@ namespace LeanCloud.Realtime.Internal.Controller {
return NewPartiallySuccessResult(allowedIds, errors);
}
internal async Task<LCIMPartiallySuccessResult> RemoveMembers(string convId, IEnumerable<string> removeIds) {
internal async Task<LCIMPartiallySuccessResult> RemoveMembers(string convId,
IEnumerable<string> removeIds) {
ConvCommand conv = new ConvCommand {
Cid = convId,
};
@ -188,7 +182,8 @@ namespace LeanCloud.Realtime.Internal.Controller {
await Client.Connection.SendRequest(request);
}
internal async Task<LCIMPartiallySuccessResult> MuteMembers(string convId, IEnumerable<string> clientIds) {
internal async Task<LCIMPartiallySuccessResult> MuteMembers(string convId,
IEnumerable<string> 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<LCIMPartiallySuccessResult> UnmuteMembers(string convId, IEnumerable<string> clientIds) {
internal async Task<LCIMPartiallySuccessResult> UnmuteMembers(string convId,
IEnumerable<string> 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<LCIMPartiallySuccessResult> BlockMembers(string convId, IEnumerable<string> clientIds) {
internal async Task<LCIMPartiallySuccessResult> BlockMembers(string convId,
IEnumerable<string> 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<LCIMPartiallySuccessResult> UnblockMembers(string convId, IEnumerable<string> clientIds) {
internal async Task<LCIMPartiallySuccessResult> UnblockMembers(string convId,
IEnumerable<string> 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<LCIMPageResult> QueryMutedMembers(string convId, int limit = 10, string next = null) {
internal async Task<LCIMPageResult> 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<LCIMPageResult> QueryBlockedMembers(string convId, int limit = 10, string next = null) {
internal async Task<LCIMPageResult> 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<string> succesfulIds, IEnumerable<ErrorCommand> errors) {
internal async Task<List<LCIMConversation>> 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<object> convs = JsonConvert.DeserializeObject<List<object>>(results.Data, new LCJsonConverter());
List<LCIMConversation> convList = convs.Select(item => {
Dictionary<string, object> conv = item as Dictionary<string, object>;
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<List<LCIMTemporaryConversation>> GetTemporaryConversations(IEnumerable<string> 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<object> convs = JsonConvert.DeserializeObject<List<object>>(results.Data, new LCJsonConverter());
List<LCIMTemporaryConversation> convList = convs.Select(item => {
LCIMTemporaryConversation temporaryConversation = new LCIMTemporaryConversation(Client);
temporaryConversation.MergeFrom(item as Dictionary<string, object>);
return temporaryConversation;
}).ToList();
return convList;
}
private LCIMPartiallySuccessResult NewPartiallySuccessResult(IEnumerable<string> succesfulIds,
IEnumerable<ErrorCommand> errors) {
LCIMPartiallySuccessResult result = new LCIMPartiallySuccessResult {
SuccessfulClientIdList = succesfulIds.ToList()
};

View File

@ -8,7 +8,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
}
internal override async Task OnNotification(GenericCommand notification) {
// TODO 清空缓存,断开连接,等待重新连接
// 清空缓存,断开连接,等待重新连接
Connection.Router.Reset();
await Connection.Close();
}

View File

@ -12,7 +12,8 @@ namespace LeanCloud.Realtime.Internal.Controller {
}
internal async Task<LCIMMessage> Send(string convId, LCIMMessage message) {
internal async Task<LCIMMessage> 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<string, object> msg = JsonConvert.DeserializeObject<Dictionary<string, object>>(messageData,
Dictionary<string, object> msgData = JsonConvert.DeserializeObject<Dictionary<string, object>>(messageData,
new LCJsonConverter());
int msgType = (int)(long)msg["_lctype"];
int msgType = (int)msgData["_lctype"];
switch (msgType) {
case -1:
message = new LCIMTextMessage();

View File

@ -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<LCIMConversation> conversationList = new List<LCIMConversation>();
foreach (UnreadTuple conv in unread.Convs) {
// 查询对话
LCIMConversation conversation = await Client.GetOrQueryConversation(conv.Cid);
IEnumerable<string> convIds = unread.Convs
.Select(conv => conv.Cid);
Dictionary<string, LCIMConversation> conversations = (await Client.GetConversationList(convIds))
.ToDictionary(item => item.Id);
List<LCIMConversation> conversationList = unread.Convs.Select(conv => {
LCIMConversation conversation = conversations[conv.Cid];
conversation.Unread = conv.Unread;
// TODO 反序列化对话
// 最后一条消息
JsonConvert.DeserializeObject<Dictionary<string, object>>(conv.Data);
conversationList.Add(conversation);
}
// 解析最后一条消息
Dictionary<string, object> msgData = JsonConvert.DeserializeObject<Dictionary<string, object>>(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);
}
}

View File

@ -300,7 +300,7 @@ namespace LeanCloud.Realtime {
}
/// <summary>
/// 获取某个特定的对话
/// 根据 id 获取对话
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
@ -308,10 +308,17 @@ namespace LeanCloud.Realtime {
if (string.IsNullOrEmpty(id)) {
throw new ArgumentNullException(nameof(id));
}
if (LCIMConversation.IsTemporayConversation(id)) {
List<LCIMTemporaryConversation> 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<LCIMConversation> results = await query.Find();
List<LCIMConversation> 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<LCIMConversation> conversationList = new List<LCIMConversation>();
foreach (string id in ids) {
LCIMConversation conversation = await GetConversation(id);
conversationList.Add(conversation);
}
return conversationList;
// 区分临时对话
IEnumerable<string> tempConvIds = ids.Where(item => {
return LCIMConversation.IsTemporayConversation(item);
});
IEnumerable<string> convIds = ids.Where(item => {
return !tempConvIds.Contains(item);
});
List<LCIMTemporaryConversation> temporaryConversations = await ConversationController.GetTemporaryConversations(tempConvIds);
LCIMConversationQuery query = GetQuery()
.WhereContainedIn("objectId", convIds)
.Limit(999);
List<LCIMConversation> conversations = await ConversationController.Find(query);
conversations.AddRange(temporaryConversations);
return conversations;
}
/// <summary>
@ -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<LCIMConversation> GetOrQueryConversation(string convId) {
if (ConversationDict.TryGetValue(convId, out LCIMConversation conversation)) {
return conversation;

View File

@ -82,7 +82,7 @@ namespace LeanCloud.Realtime {
ConversationId = direct.Cid;
Id = direct.Id;
FromClientId = direct.FromPeerId;
DeliveredTimestamp = direct.Timestamp;
SentTimestamp = direct.Timestamp;
}
}
}

View File

@ -55,7 +55,7 @@ namespace LeanCloud.Realtime {
internal static LCIMTypedMessage Deserialize(Dictionary<string, object> messageData) {
LCIMTypedMessage message = null;
int msgType = (int)(long)messageData["_lctype"];
int msgType = (int)messageData["_lctype"];
switch (msgType) {
case -1:
message = new LCIMTextMessage();

View File

@ -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<LCIMConversation> 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);