2020-03-12 16:23:21 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-03-27 15:52:34 +08:00
|
|
|
|
using System.Collections.ObjectModel;
|
2020-03-12 16:23:21 +08:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using LeanCloud;
|
|
|
|
|
using LeanCloud.Common;
|
|
|
|
|
using LeanCloud.Realtime;
|
|
|
|
|
|
|
|
|
|
namespace RealtimeConsole {
|
|
|
|
|
class MainClass {
|
|
|
|
|
public static void Main(string[] args) {
|
2020-03-26 16:08:35 +08:00
|
|
|
|
Console.WriteLine($"Hello World at {Thread.CurrentThread.ManagedThreadId}");
|
2020-03-12 16:23:21 +08:00
|
|
|
|
|
|
|
|
|
LCLogger.LogDelegate += (level, info) => {
|
|
|
|
|
switch (level) {
|
2020-04-13 10:47:14 +08:00
|
|
|
|
case LCLogLevel.Debug: {
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
|
|
|
|
Console.WriteLine($"{DateTime.Now} [DEBUG]\n{info}");
|
|
|
|
|
Console.ResetColor();
|
|
|
|
|
}
|
2020-03-12 16:23:21 +08:00
|
|
|
|
break;
|
2020-04-13 10:47:14 +08:00
|
|
|
|
case LCLogLevel.Warn: {
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
|
|
|
|
Console.WriteLine($"{DateTime.Now} [WARNING]\n{info}");
|
|
|
|
|
Console.ResetColor();
|
|
|
|
|
}
|
2020-03-12 16:23:21 +08:00
|
|
|
|
break;
|
2020-04-13 10:47:14 +08:00
|
|
|
|
case LCLogLevel.Error: {
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
|
|
|
Console.WriteLine($"{DateTime.Now} [ERROR]\n{info}");
|
|
|
|
|
Console.ResetColor();
|
|
|
|
|
}
|
2020-03-12 16:23:21 +08:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Console.WriteLine(info);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
|
|
|
|
|
|
2020-03-26 16:08:35 +08:00
|
|
|
|
SingleThreadSynchronizationContext.Run(async () => {
|
|
|
|
|
Console.WriteLine($"start at {Thread.CurrentThread.ManagedThreadId}");
|
2020-04-13 10:47:14 +08:00
|
|
|
|
//await Run("cc1");
|
2020-03-26 16:08:35 +08:00
|
|
|
|
//await ChatRoom();
|
|
|
|
|
//await TemporaryConversation();
|
|
|
|
|
//await CreateConversation();
|
|
|
|
|
//await QueryMyConversation();
|
2020-04-13 10:47:14 +08:00
|
|
|
|
//await AutoSendMessage();
|
|
|
|
|
|
|
|
|
|
//await KeepAlive();
|
|
|
|
|
|
|
|
|
|
await OpenAndClose();
|
|
|
|
|
|
2020-03-26 16:08:35 +08:00
|
|
|
|
Console.WriteLine($"done at {Thread.CurrentThread.ManagedThreadId}");
|
|
|
|
|
});
|
2020-03-19 15:12:50 +08:00
|
|
|
|
|
2020-04-13 10:47:14 +08:00
|
|
|
|
//AutoSendMessage().Wait();
|
|
|
|
|
|
2020-03-26 16:08:35 +08:00
|
|
|
|
//Conversation().Wait();
|
2020-03-24 17:42:04 +08:00
|
|
|
|
|
|
|
|
|
//_ = Signature();
|
|
|
|
|
|
|
|
|
|
//_ = Block();
|
|
|
|
|
|
|
|
|
|
//_ = Mute();
|
|
|
|
|
|
|
|
|
|
//QueryConversation().Wait();
|
|
|
|
|
|
|
|
|
|
//_ = OpenAndClose();
|
|
|
|
|
|
2020-03-26 16:08:35 +08:00
|
|
|
|
//SendMessage().Wait();
|
2020-03-24 17:42:04 +08:00
|
|
|
|
|
2020-03-25 16:42:30 +08:00
|
|
|
|
//Unread().Wait();
|
2020-03-19 15:12:50 +08:00
|
|
|
|
|
2020-03-27 15:52:34 +08:00
|
|
|
|
//DemoAsync().Wait();
|
|
|
|
|
|
|
|
|
|
//SingleThreadSynchronizationContext.Run(async () => {
|
|
|
|
|
// await DemoAsync();
|
|
|
|
|
//});
|
|
|
|
|
|
2020-03-19 15:12:50 +08:00
|
|
|
|
Console.ReadKey(true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-13 10:47:14 +08:00
|
|
|
|
static async Task KeepAlive() {
|
|
|
|
|
LCIMClient client = new LCIMClient("cc1");
|
|
|
|
|
await client.Open();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async Task AutoSendMessage() {
|
|
|
|
|
LCIMClient client = new LCIMClient("cc1");
|
|
|
|
|
await client.Open();
|
|
|
|
|
LCIMConversation conversation = await client.CreateConversation(new string[] { "cc2", "cc3", "cc5" });
|
|
|
|
|
int count = 0;
|
|
|
|
|
while (count < 10) {
|
|
|
|
|
LCIMTextMessage textMessage = new LCIMTextMessage($"hello, {count}");
|
|
|
|
|
await conversation.Send(textMessage);
|
|
|
|
|
await Task.Delay(5000);
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-27 15:52:34 +08:00
|
|
|
|
static async Task DemoAsync() {
|
|
|
|
|
Dictionary<int, int> d = new Dictionary<int, int>();
|
|
|
|
|
for (int i = 0; i < 10000; i++) {
|
|
|
|
|
int id = Thread.CurrentThread.ManagedThreadId;
|
|
|
|
|
int count;
|
|
|
|
|
d[id] = d.TryGetValue(id, out count) ? count + 1 : 1;
|
|
|
|
|
await Task.Yield();
|
|
|
|
|
}
|
|
|
|
|
foreach (KeyValuePair<int, int> kv in d) {
|
|
|
|
|
Console.WriteLine(kv);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 16:08:35 +08:00
|
|
|
|
static async Task Run(string id) {
|
|
|
|
|
LCIMClient client = new LCIMClient(id);
|
|
|
|
|
await client.Open();
|
2020-03-27 15:52:34 +08:00
|
|
|
|
client.OnUnreadMessagesCountUpdated = (conversations) => {
|
|
|
|
|
foreach (LCIMConversation conv in conversations) {
|
|
|
|
|
Console.WriteLine($"unread: {conv.Id}");
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-04-13 10:47:14 +08:00
|
|
|
|
client.OnMessage = async (conversation, message) => {
|
2020-03-26 16:08:35 +08:00
|
|
|
|
Console.WriteLine($"recv: {conversation.Id}, {message.Id} at {Thread.CurrentThread.ManagedThreadId}");
|
2020-04-13 10:47:14 +08:00
|
|
|
|
await conversation.Read();
|
2020-03-26 16:08:35 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async Task CreateConversation() {
|
|
|
|
|
LCIMClient cc1 = new LCIMClient("cc1");
|
|
|
|
|
await cc1.Open();
|
2020-04-13 10:47:14 +08:00
|
|
|
|
await cc1.CreateConversation(new string[] { "cc2", "cc3", "cc5" });
|
2020-03-26 16:08:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async Task QueryMyConversation() {
|
|
|
|
|
LCIMClient cc1 = new LCIMClient("cc1");
|
|
|
|
|
await cc1.Open();
|
2020-03-27 15:52:34 +08:00
|
|
|
|
ReadOnlyCollection<LCIMConversation> conversationList = await cc1.GetQuery()
|
2020-03-26 16:08:35 +08:00
|
|
|
|
.WhereEqualTo("objectId", "5e7c283790aef5aa846b5683")
|
|
|
|
|
.Find();
|
2020-03-27 15:52:34 +08:00
|
|
|
|
foreach (LCIMConversation conv in conversationList) {
|
2020-03-26 16:08:35 +08:00
|
|
|
|
Console.WriteLine($"convId: {conv.Id}");
|
2020-03-27 15:52:34 +08:00
|
|
|
|
}
|
2020-03-24 17:42:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async Task Unread() {
|
|
|
|
|
LCIMClient u2 = new LCIMClient("u2");
|
|
|
|
|
await u2.Open();
|
|
|
|
|
u2.OnUnreadMessagesCountUpdated = conversationList => {
|
|
|
|
|
foreach (LCIMConversation conv in conversationList) {
|
|
|
|
|
Console.WriteLine($"unread: {conv.Unread}");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async Task SendMessage() {
|
2020-03-26 16:08:35 +08:00
|
|
|
|
Console.WriteLine($"start at {Thread.CurrentThread.ManagedThreadId}");
|
2020-03-24 17:42:04 +08:00
|
|
|
|
try {
|
|
|
|
|
LCIMClient u1 = new LCIMClient("u1");
|
|
|
|
|
await u1.Open();
|
|
|
|
|
LCIMConversation conversation = await u1.CreateConversation(new string[] { "u2" });
|
|
|
|
|
|
2020-03-26 16:08:35 +08:00
|
|
|
|
Console.WriteLine($"open at {Thread.CurrentThread.ManagedThreadId}");
|
|
|
|
|
|
2020-03-24 17:42:04 +08:00
|
|
|
|
LCIMTextMessage textMessage = new LCIMTextMessage("hello, text message");
|
|
|
|
|
await conversation.Send(textMessage);
|
|
|
|
|
|
2020-03-26 16:08:35 +08:00
|
|
|
|
Console.WriteLine($"send at {Thread.CurrentThread.ManagedThreadId}");
|
|
|
|
|
|
2020-03-24 17:42:04 +08:00
|
|
|
|
//LCFile file = new LCFile("avatar", "../../../Storage.Test/assets/hello.png");
|
|
|
|
|
//await file.Save();
|
|
|
|
|
//LCIMImageMessage imageMessage = new LCIMImageMessage(file);
|
|
|
|
|
//await conversation.Send(imageMessage);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Console.WriteLine(e.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async Task OpenAndClose() {
|
|
|
|
|
LCIMClient o1 = new LCIMClient("o1");
|
|
|
|
|
await o1.Open();
|
2020-04-13 10:47:14 +08:00
|
|
|
|
await Task.Delay(30000);
|
2020-03-24 17:42:04 +08:00
|
|
|
|
await o1.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async Task QueryConversation() {
|
|
|
|
|
LCIMClient m2 = new LCIMClient("m2");
|
|
|
|
|
await m2.Open();
|
|
|
|
|
|
|
|
|
|
LCIMConversation conv = (await m2.GetQuery()
|
|
|
|
|
.WhereEqualTo("objectId", "5e7863bf90aef5aa849be75a")
|
|
|
|
|
.Find())[0];
|
|
|
|
|
LCIMTextMessage textMessage = new LCIMTextMessage("hello, world");
|
|
|
|
|
await conv.Send(textMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async Task Mute() {
|
|
|
|
|
LCIMClient m1 = new LCIMClient("m0");
|
|
|
|
|
await m1.Open();
|
|
|
|
|
|
|
|
|
|
LCIMClient m2 = new LCIMClient("m2");
|
|
|
|
|
await m2.Open();
|
|
|
|
|
|
|
|
|
|
LCIMConversation conversation = await m1.CreateConversation(new string[] { "m2", "m3" });
|
|
|
|
|
await conversation.MuteMembers(new string[] { "m2" });
|
|
|
|
|
|
|
|
|
|
LCIMConversation conv = (await m2.GetQuery()
|
|
|
|
|
.WhereEqualTo("objectId", conversation.Id)
|
|
|
|
|
.Find())[0];
|
|
|
|
|
LCIMTextMessage textMessage = new LCIMTextMessage("hello, world");
|
|
|
|
|
await conv.Send(textMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async Task Block() {
|
|
|
|
|
LocalSignatureFactory signatureFactory = new LocalSignatureFactory();
|
|
|
|
|
LCIMClient c1 = new LCIMClient("c0");
|
|
|
|
|
await c1.Open();
|
|
|
|
|
LCIMConversation conversation = await c1.CreateConversation(new string[] { "c2", "c3", "c4", "c5" });
|
|
|
|
|
LCIMTextMessage textMessage = new LCIMTextMessage("hello");
|
|
|
|
|
await conversation.Send(textMessage);
|
|
|
|
|
await conversation.BlockMembers(new string[] { "c5" });
|
|
|
|
|
|
|
|
|
|
LCIMClient c5 = new LCIMClient("c5");
|
|
|
|
|
await c5.Open();
|
2020-03-25 16:42:30 +08:00
|
|
|
|
await conversation.AddMembers(new string[] { "c5" });
|
2020-03-24 17:42:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async Task Signature() {
|
|
|
|
|
LocalSignatureFactory signatureFactory = new LocalSignatureFactory();
|
|
|
|
|
LCIMClient hello = new LCIMClient("hello111", signatureFactory);
|
|
|
|
|
await hello.Open();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-19 15:12:50 +08:00
|
|
|
|
static async Task ChatRoom() {
|
2020-03-24 17:42:04 +08:00
|
|
|
|
LocalSignatureFactory signatureFactory = new LocalSignatureFactory();
|
|
|
|
|
LCIMClient hello = new LCIMClient("hello", signatureFactory);
|
2020-03-19 15:12:50 +08:00
|
|
|
|
await hello.Open();
|
|
|
|
|
|
|
|
|
|
string name = Guid.NewGuid().ToString();
|
|
|
|
|
LCIMChatRoom chatRoom = await hello.CreateChatRoom(name);
|
|
|
|
|
Console.WriteLine(chatRoom.Name);
|
2020-03-24 17:42:04 +08:00
|
|
|
|
|
2020-03-25 16:42:30 +08:00
|
|
|
|
await chatRoom.AddMembers(new string[] { "world" });
|
2020-03-24 17:42:04 +08:00
|
|
|
|
|
2020-03-25 16:42:30 +08:00
|
|
|
|
await chatRoom.RemoveMembers(new string[] { "world" });
|
2020-03-19 15:12:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 17:42:04 +08:00
|
|
|
|
static async Task Conversation() {
|
2020-03-18 16:21:29 +08:00
|
|
|
|
LCIMClient hello = new LCIMClient("hello");
|
2020-03-12 16:23:21 +08:00
|
|
|
|
|
2020-03-18 16:21:29 +08:00
|
|
|
|
await hello.Open();
|
2020-03-12 16:23:21 +08:00
|
|
|
|
|
2020-03-18 16:21:29 +08:00
|
|
|
|
hello.OnInvited = (conv, initBy) => {
|
2020-03-12 16:23:21 +08:00
|
|
|
|
Console.WriteLine($"on invited: {initBy}");
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-18 16:21:29 +08:00
|
|
|
|
hello.OnMembersJoined = (conv, memberList, initBy) => {
|
2020-03-12 16:23:21 +08:00
|
|
|
|
Console.WriteLine($"on members joined: {initBy}");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
List<string> memberIdList = new List<string> { "world", "code" };
|
|
|
|
|
string name = Guid.NewGuid().ToString();
|
2020-03-18 16:21:29 +08:00
|
|
|
|
LCIMConversation conversation = await hello.CreateConversation(memberIdList, name: name, unique: true);
|
|
|
|
|
|
|
|
|
|
LCIMClient world = new LCIMClient("world");
|
|
|
|
|
await world.Open();
|
|
|
|
|
|
2020-03-25 16:42:30 +08:00
|
|
|
|
world.OnMessage = (conv, message) => {
|
2020-03-18 16:21:29 +08:00
|
|
|
|
Console.WriteLine(message);
|
2020-03-19 11:04:37 +08:00
|
|
|
|
if (message is LCIMTypedMessage typedMessage) {
|
|
|
|
|
Console.WriteLine(typedMessage["k1"]);
|
|
|
|
|
Console.WriteLine(typedMessage["k2"]);
|
|
|
|
|
Console.WriteLine(typedMessage["k3"]);
|
|
|
|
|
}
|
2020-03-18 16:21:29 +08:00
|
|
|
|
};
|
2020-03-16 11:50:49 +08:00
|
|
|
|
|
2020-03-18 11:41:13 +08:00
|
|
|
|
//LCIMTextMessage textMessage = new LCIMTextMessage("hello, world");
|
|
|
|
|
//await conversation.Send(textMessage);
|
2020-03-17 16:17:19 +08:00
|
|
|
|
|
2020-03-18 11:41:13 +08:00
|
|
|
|
//await Task.Delay(3000);
|
2020-03-17 16:17:19 +08:00
|
|
|
|
|
2020-03-18 11:41:13 +08:00
|
|
|
|
//LCIMTextMessage newMessage = new LCIMTextMessage("hello, code");
|
|
|
|
|
//await conversation.Update(textMessage, newMessage);
|
2020-03-17 16:17:19 +08:00
|
|
|
|
|
|
|
|
|
//// 设置成员的角色
|
|
|
|
|
//await conversation.UpdateMemberRole("world", LCIMConversationMemberInfo.Manager);
|
|
|
|
|
|
|
|
|
|
//List<LCIMConversationMemberInfo> members = await conversation.GetAllMemberInfo();
|
|
|
|
|
|
|
|
|
|
//foreach (LCIMConversationMemberInfo member in members) {
|
|
|
|
|
// Console.WriteLine(member.MemberId);
|
|
|
|
|
//}
|
|
|
|
|
|
2020-03-19 11:04:37 +08:00
|
|
|
|
LCIMTextMessage textMessage = new LCIMTextMessage("hello, world");
|
|
|
|
|
textMessage["k1"] = 123;
|
|
|
|
|
textMessage["k2"] = "abc";
|
|
|
|
|
textMessage["k3"] = true;
|
|
|
|
|
await conversation.Send(textMessage);
|
2020-03-18 11:41:13 +08:00
|
|
|
|
|
2020-03-18 16:21:29 +08:00
|
|
|
|
//LCFile file = new LCFile("avatar", "../../../Storage.Test/assets/hello.png");
|
|
|
|
|
//file.MetaData["width"] = 225;
|
|
|
|
|
//file.MetaData["height"] = 225;
|
|
|
|
|
//file.MetaData["size"] = 1186;
|
|
|
|
|
//await file.Save();
|
|
|
|
|
//LCIMImageMessage imageMessage = new LCIMImageMessage(file);
|
|
|
|
|
//await conversation.Send(imageMessage);
|
2020-03-18 11:41:13 +08:00
|
|
|
|
|
2020-03-19 11:04:37 +08:00
|
|
|
|
//LCGeoPoint location = new LCGeoPoint(11, 12);
|
|
|
|
|
//LCIMLocationMessage locationMessage = new LCIMLocationMessage(location);
|
|
|
|
|
//await conversation.Send(locationMessage);
|
2020-03-12 16:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|