* Client.cs:

* Conversation.cs:
* ConversationQuery.cs:
* LocalSignatureFactory.cs:

* Message.cs: test
oneRain 2020-04-24 17:42:45 +08:00
parent ae8fbfa830
commit 659da6acc8
5 changed files with 362 additions and 102 deletions

View File

@ -1,7 +1,135 @@
using System; using NUnit.Framework;
using System;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using LeanCloud;
using LeanCloud.Common;
using LeanCloud.Realtime;
using LeanCloud.Storage;
using static NUnit.Framework.TestContext;
namespace Realtime.Test { namespace Realtime.Test {
public class Client { public class Client {
public Client() { [SetUp]
public void SetUp() {
LCLogger.LogDelegate += Utils.Print;
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
}
[TearDown]
public void TearDown() {
LCLogger.LogDelegate -= Utils.Print;
}
[Test]
public async Task OpenAndClose() {
LCIMClient client = new LCIMClient("c1");
await client.Open();
await client.Close();
}
[Test]
public async Task OpenAndCloseByLCUser() {
LCUser user = await LCUser.Login("hello", "world");
LCIMClient client = new LCIMClient(user);
await client.Open();
await client.Close();
}
[Test]
public async Task CreateConversation() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
string clientId = Guid.NewGuid().ToString();
LCIMClient client = new LCIMClient(clientId);
await client.Open();
client.OnInvited = (conv, initBy) => {
WriteLine($"on invited: {initBy}");
WriteLine(conv.CreatorId);
};
client.OnMembersJoined = (conv, memberList, initBy) => {
WriteLine($"on members joined: {initBy}");
foreach (string memberId in conv.MemberIds) {
WriteLine(memberId);
}
tcs.SetResult(null);
};
string name = Guid.NewGuid().ToString();
LCIMConversation conversation = await client.CreateConversation(new string[] { "world" }, name: name, unique: false);
await tcs.Task;
}
[Test]
public async Task CreateChatRoom() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
string clientId = Guid.NewGuid().ToString();
LCIMClient client = new LCIMClient(clientId);
await client.Open();
client.OnInvited = (conv, initBy) => {
WriteLine($"on invited: {initBy}");
};
string name = Guid.NewGuid().ToString();
LCIMConversation conversation = await client.CreateChatRoom(name);
string visitorId = Guid.NewGuid().ToString();
LCIMClient visitor = new LCIMClient(visitorId);
await visitor.Open();
visitor.OnInvited = async (conv, initBy) => {
WriteLine($"on invited: {visitor.Id}");
LCIMTextMessage textMessage = new LCIMTextMessage("hello, world");
await conversation.Send(textMessage);
tcs.SetResult(null);
};
LCIMChatRoom chatRoom = await visitor.GetConversation(conversation.Id) as LCIMChatRoom;
await chatRoom.Join();
int count = await chatRoom.GetMembersCount();
ReadOnlyCollection<string> onlineMembers = await chatRoom.GetOnlineMembers();
Assert.GreaterOrEqual(onlineMembers.Count, 1);
foreach (string memberId in onlineMembers) {
WriteLine($"{memberId} online");
}
await client.Close();
await visitor.Close();
await tcs.Task;
}
[Test]
public async Task CreateTemporaryConversation() {
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
string clientId = Guid.NewGuid().ToString();
LCIMClient client = new LCIMClient(clientId);
await client.Open();
client.OnInvited = (conv, initBy) => {
WriteLine($"on invited: {initBy}");
};
client.OnMembersJoined = (conv, memberList, initBy) => {
WriteLine($"on members joined: {initBy}");
tcs.SetResult(null);
};
await client.CreateTemporaryConversation(new string[] { "world" });
await tcs.Task;
} }
} }
} }

View File

@ -8,115 +8,88 @@ using LeanCloud.Realtime;
namespace Realtime.Test { namespace Realtime.Test {
public class Conversation { public class Conversation {
private LCIMClient c1;
private LCIMClient c2;
private LCIMConversation conversation;
[SetUp] [SetUp]
public void SetUp() { public async Task SetUp() {
LCLogger.LogDelegate += Utils.Print; LCLogger.LogDelegate += Utils.Print;
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com"); LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
c1 = new LCIMClient(Guid.NewGuid().ToString());
await c1.Open();
c2 = new LCIMClient(Guid.NewGuid().ToString());
await c2.Open();
conversation = await c1.CreateConversation(new string[] { "lean", "cloud" });
} }
[TearDown] [TearDown]
public void TearDown() { public async Task TearDown() {
await c1.Close();
await c2.Close();
LCLogger.LogDelegate -= Utils.Print; LCLogger.LogDelegate -= Utils.Print;
} }
[Test] [Test]
public async Task CreateConversation() { [Order(0)]
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(); public async Task AddMember() {
await conversation.AddMembers(new string[] { c2.Id });
string clientId = Guid.NewGuid().ToString(); Assert.AreEqual(conversation.MemberIds.Count, 4);
LCIMClient client = new LCIMClient(clientId);
await client.Open();
client.OnInvited = (conv, initBy) => {
TestContext.WriteLine($"on invited: {initBy}");
TestContext.WriteLine(conv.CreatorId);
};
client.OnMembersJoined = (conv, memberList, initBy) => {
TestContext.WriteLine($"on members joined: {initBy}");
foreach (string memberId in conv.MemberIds) {
TestContext.WriteLine(memberId);
}
tcs.SetResult(null);
};
List<string> memberIdList = new List<string> { "world" };
string name = Guid.NewGuid().ToString();
await client.CreateConversation(memberIdList, name: name, unique: false);
await tcs.Task;
} }
[Test] [Test]
public async Task CreateChatRoom() { [Order(1)]
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(); public async Task MuteMembers() {
await conversation.MuteMembers(new string[] { "lean" });
string clientId = Guid.NewGuid().ToString(); Assert.True(conversation.MutedMemberIds.Contains("lean"));
LCIMClient client = new LCIMClient(clientId);
await client.Open();
client.OnInvited = (conv, initBy) => {
TestContext.WriteLine($"on invited: {initBy}");
tcs.SetResult(null);
};
string name = Guid.NewGuid().ToString();
await client.CreateChatRoom(name);
await tcs.Task;
} }
[Test] [Test]
public async Task CreateTemporaryConversation() { [Order(2)]
TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(); public async Task UnmuteMembers() {
await conversation.UnmuteMembers(new string[] { "lean" });
string clientId = Guid.NewGuid().ToString(); Assert.False(conversation.MutedMemberIds.Contains("lean"));
LCIMClient client = new LCIMClient(clientId);
await client.Open();
client.OnInvited = (conv, initBy) => {
TestContext.WriteLine($"on invited: {initBy}");
};
client.OnMembersJoined = (conv, memberList, initBy) => {
TestContext.WriteLine($"on members joined: {initBy}");
tcs.SetResult(null);
};
List<string> memberIdList = new List<string> { "world" };
await client.CreateTemporaryConversation(memberIdList);
await tcs.Task;
} }
[Test] [Test]
public async Task Query() { [Order(3)]
LCIMClient client = new LCIMClient("hello123"); public async Task BlockMembers() {
await client.Open(); await conversation.BlockMembers(new string[] { "lean" });
LCIMPageResult result = await conversation.QueryBlockedMembers();
LCIMConversationQuery query = new LCIMConversationQuery(client); Assert.True(result.Results.Contains("lean"));
await query.Find();
} }
[Test] [Test]
public async Task Save() { [Order(4)]
string clientId = Guid.NewGuid().ToString(); public async Task UnblockMembers() {
LCIMClient client = new LCIMClient(clientId); await conversation.UnblockMembers(new string[] { "lean" });
LCIMPageResult result = await conversation.QueryBlockedMembers();
Assert.False(result.Results.Contains("lean"));
}
await client.Open(); [Test]
[Order(5)]
public async Task UpdateRole() {
await conversation.UpdateMemberRole("cloud", LCIMConversationMemberInfo.Manager);
LCIMConversationMemberInfo memberInfo = await conversation.GetMemberInfo("cloud");
Assert.True(memberInfo.IsManager);
}
string otherId = Guid.NewGuid().ToString(); [Test]
LCIMConversation conversation = await client.CreateConversation(new List<string> { otherId }); [Order(6)]
public async Task RemoveMember() {
await conversation.RemoveMembers(new string[] { c2.Id });
Assert.AreEqual(conversation.MemberIds.Count, 3);
}
[Test]
[Order(7)]
public async Task UpdateInfo() {
await conversation.UpdateInfo(new Dictionary<string, object> { await conversation.UpdateInfo(new Dictionary<string, object> {
{ "name", "leancloud" }, { "name", "leancloud" },
{ "k1", "v1" }, { "k1", "v1" },
{ "k2", "v2" } { "k2", "v2" }
}); });
Assert.AreEqual(conversation.Name, "leancloud"); Assert.AreEqual(conversation.Name, "leancloud");
Assert.AreEqual(conversation["k1"], "v1"); Assert.AreEqual(conversation["k1"], "v1");
Assert.AreEqual(conversation["k2"], "v2"); Assert.AreEqual(conversation["k2"], "v2");

View File

@ -1,7 +1,49 @@
using System; using NUnit.Framework;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using LeanCloud;
using LeanCloud.Common;
using LeanCloud.Realtime;
namespace Realtime.Test { namespace Realtime.Test {
public class ConversationQuery { public class ConversationQuery {
public ConversationQuery() { private string clientId = "hello123";
private LCIMClient client;
[SetUp]
public async Task SetUp() {
LCLogger.LogDelegate += Utils.Print;
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
client = new LCIMClient(clientId);
await client.Open();
}
[TearDown]
public async Task TearDown() {
LCLogger.LogDelegate -= Utils.Print;
await client.Close();
}
[Test]
public async Task QueryMyConversation() {
LCIMConversationQuery query = new LCIMConversationQuery(client);
ReadOnlyCollection<LCIMConversation> conversations = await query.Find();
Assert.Greater(conversations.Count, 0);
foreach (LCIMConversation conversation in conversations) {
Assert.True(conversation.MemberIds.Contains(clientId));
}
}
[Test]
public async Task QueryMemberConversation() {
string memberId = "cc1";
LCIMConversationQuery query = new LCIMConversationQuery(client);
query.WhereContains("m", memberId);
ReadOnlyCollection<LCIMConversation> conversations = await query.Find();
Assert.Greater(conversations.Count, 0);
foreach (LCIMConversation conversation in conversations) {
Assert.True(conversation.MemberIds.Contains(memberId));
}
} }
} }
} }

View File

@ -1,7 +1,97 @@
using System; using System;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Security.Cryptography;
using LeanCloud.Realtime;
using LeanCloud;
namespace Realtime.Test { namespace Realtime.Test {
public class LocalSignatureFactory { public class LocalSignatureFactory : ILCIMSignatureFactory {
public LocalSignatureFactory() { const string MasterKey = "pyvbNSh5jXsuFQ3C8EgnIdhw";
public Task<LCIMSignature> CreateConnectSignature(string clientId) {
long timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
string nonce = NewNonce();
string signature = GenerateSignature(LCApplication.AppId, clientId, string.Empty, timestamp.ToString(), nonce);
return Task.FromResult(new LCIMSignature {
Signature = signature,
Timestamp = timestamp,
Nonce = nonce
});
}
public Task<LCIMSignature> CreateStartConversationSignature(string clientId, IEnumerable<string> memberIds) {
string sortedMemberIds = string.Empty;
if (memberIds != null) {
List<string> sortedMemberList = memberIds.ToList();
sortedMemberList.Sort();
sortedMemberIds = string.Join(":", sortedMemberList);
}
long timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
string nonce = NewNonce();
string signature = GenerateSignature(LCApplication.AppId, clientId, sortedMemberIds, timestamp.ToString(), nonce);
return Task.FromResult(new LCIMSignature {
Signature = signature,
Timestamp = timestamp,
Nonce = nonce
});
}
public Task<LCIMSignature> CreateConversationSignature(string conversationId, string clientId, IEnumerable<string> memberIds, string action) {
string sortedMemberIds = string.Empty;
if (memberIds != null) {
List<string> sortedMemberList = memberIds.ToList();
sortedMemberList.Sort();
sortedMemberIds = string.Join(":", sortedMemberList);
}
long timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
string nonce = NewNonce();
string signature = GenerateSignature(LCApplication.AppId, clientId, conversationId, sortedMemberIds, timestamp.ToString(), nonce, action);
return Task.FromResult(new LCIMSignature {
Signature = signature,
Timestamp = timestamp,
Nonce = nonce
});
}
public Task<LCIMSignature> CreateBlacklistSignature(string conversationId, string clientId, IEnumerable<string> memberIds, string action) {
string sortedMemberIds = string.Empty;
if (memberIds != null) {
List<string> sortedMemberList = memberIds.ToList();
sortedMemberList.Sort();
sortedMemberIds = string.Join(":", sortedMemberList);
}
long timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
string nonce = NewNonce();
string signature = GenerateSignature(LCApplication.AppId, clientId, conversationId, sortedMemberIds, timestamp.ToString(), nonce, action);
return Task.FromResult(new LCIMSignature {
Signature = signature,
Timestamp = timestamp,
Nonce = nonce
});
}
private static string SignSHA1(string key, string text) {
HMACSHA1 hmac = new HMACSHA1(Encoding.UTF8.GetBytes(key));
byte[] bytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(text));
string signature = BitConverter.ToString(bytes).Replace("-", string.Empty);
return signature;
}
private static string NewNonce() {
byte[] bytes = new byte[10];
using (RandomNumberGenerator generator = RandomNumberGenerator.Create()) {
generator.GetBytes(bytes);
}
return Convert.ToBase64String(bytes);
}
private static string GenerateSignature(params string[] args) {
string text = string.Join(":", args);
string signature = SignSHA1(MasterKey, text);
return signature;
} }
} }
} }

View File

@ -1,42 +1,69 @@
using NUnit.Framework; using NUnit.Framework;
using System; using System;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections.Generic; using System.Collections.Generic;
using LeanCloud; using LeanCloud;
using LeanCloud.Common; using LeanCloud.Common;
using LeanCloud.Storage;
using LeanCloud.Realtime; using LeanCloud.Realtime;
using static System.Console;
namespace Realtime.Test { namespace Realtime.Test {
public class Message { public class Message {
private LCIMClient m1;
private LCIMClient m2;
private LCIMConversation conversation;
[SetUp] [SetUp]
public void SetUp() { public async Task SetUp() {
LCLogger.LogDelegate += Utils.Print; LCLogger.LogDelegate += Utils.Print;
LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com"); LCApplication.Initialize("ikGGdRE2YcVOemAaRbgp1xGJ-gzGzoHsz", "NUKmuRbdAhg1vrb2wexYo1jo", "https://ikggdre2.lc-cn-n1-shared.com");
m1 = new LCIMClient("m1");
m2 = new LCIMClient("m2");
await m1.Open();
await m2.Open();
conversation = await m1.CreateConversation(new string[] { "m2" });
} }
[TearDown] [TearDown]
public void TearDown() { public async Task TearDown() {
await m1.Close();
await m2.Close();
LCLogger.LogDelegate -= Utils.Print; LCLogger.LogDelegate -= Utils.Print;
} }
[Test] [Test]
public async Task Send() { public async Task Send() {
try { AutoResetEvent are = new AutoResetEvent(false);
string clientId = Guid.NewGuid().ToString(); m2.OnMessage = (conv, msg) => {
LCIMClient client = new LCIMClient(clientId); WriteLine(msg.Id);
await client.Open(); if (msg is LCIMImageMessage imageMsg) {
List<string> memberIdList = new List<string> { "world" }; WriteLine($"-------- url: {imageMsg.Url}");
string name = Guid.NewGuid().ToString(); } else if (msg is LCIMFileMessage fileMsg) {
LCIMConversation conversation = await client.CreateConversation(memberIdList, name: name, unique: false); WriteLine($"-------- name: {fileMsg.Format}");
} else if (msg is LCIMTextMessage textMsg) {
WriteLine($"-------- text: {textMsg.Text}");
}
};
LCIMTextMessage textMessage = new LCIMTextMessage("hello, world"); LCIMTextMessage textMessage = new LCIMTextMessage("hello, world");
await conversation.Send(textMessage); await conversation.Send(textMessage);
TestContext.WriteLine(textMessage.Id);
TestContext.WriteLine(textMessage.DeliveredAt);
Assert.NotNull(textMessage.Id); Assert.NotNull(textMessage.Id);
} catch (Exception e) {
LCLogger.Error(e.Message); LCFile image = new LCFile("hello", "../../../../assets/hello.png");
} await image.Save();
LCIMImageMessage imageMessage = new LCIMImageMessage(image);
await conversation.Send(imageMessage);
Assert.NotNull(imageMessage.Id);
LCFile file = new LCFile("apk", "../../../../assets/test.apk");
await file.Save();
LCIMFileMessage fileMessage = new LCIMFileMessage(file);
await conversation.Send(fileMessage);
Assert.NotNull(fileMessage.Id);
} }
} }
} }