diff --git a/Realtime/Internal/Controller/LCIMConversationController.cs b/Realtime/Internal/Controller/LCIMConversationController.cs index caee6fe..79c9f71 100644 --- a/Realtime/Internal/Controller/LCIMConversationController.cs +++ b/Realtime/Internal/Controller/LCIMConversationController.cs @@ -57,7 +57,7 @@ namespace LeanCloud.Realtime.Internal.Controller { }; } if (Client.SignatureFactory != null) { - LCIMSignature signature = Client.SignatureFactory.CreateStartConversationSignature(Client.Id, members); + LCIMSignature signature = await Client.SignatureFactory.CreateStartConversationSignature(Client.Id, members); conv.S = signature.Signature; conv.T = signature.Timestamp; conv.N = signature.Nonce; @@ -145,7 +145,7 @@ namespace LeanCloud.Realtime.Internal.Controller { conv.M.AddRange(clientIds); // 签名参数 if (Client.SignatureFactory != null) { - LCIMSignature signature = Client.SignatureFactory.CreateConversationSignature(convId, + LCIMSignature signature = await Client.SignatureFactory.CreateConversationSignature(convId, Client.Id, clientIds, LCIMSignatureAction.Invite); @@ -175,7 +175,7 @@ namespace LeanCloud.Realtime.Internal.Controller { conv.M.AddRange(removeIds); // 签名参数 if (Client.SignatureFactory != null) { - LCIMSignature signature = Client.SignatureFactory.CreateConversationSignature(convId, + LCIMSignature signature = await Client.SignatureFactory.CreateConversationSignature(convId, Client.Id, removeIds, LCIMSignatureAction.Kick); @@ -271,7 +271,7 @@ namespace LeanCloud.Realtime.Internal.Controller { }; blacklist.ToPids.AddRange(clientIds); if (Client.SignatureFactory != null) { - LCIMSignature signature = Client.SignatureFactory.CreateBlacklistSignature(convId, + LCIMSignature signature = await Client.SignatureFactory.CreateBlacklistSignature(convId, Client.Id, clientIds, LCIMSignatureAction.ConversationBlockClients); @@ -298,7 +298,7 @@ namespace LeanCloud.Realtime.Internal.Controller { }; blacklist.ToPids.AddRange(clientIds); if (Client.SignatureFactory != null) { - LCIMSignature signature = Client.SignatureFactory.CreateBlacklistSignature(convId, + LCIMSignature signature = await Client.SignatureFactory.CreateBlacklistSignature(convId, Client.Id, clientIds, LCIMSignatureAction.ConversationUnblockClients); diff --git a/Realtime/Internal/Controller/LCIMSessionController.cs b/Realtime/Internal/Controller/LCIMSessionController.cs index 336dbb9..6017ca6 100644 --- a/Realtime/Internal/Controller/LCIMSessionController.cs +++ b/Realtime/Internal/Controller/LCIMSessionController.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; +using LeanCloud.Storage; using LeanCloud.Realtime.Protocol; namespace LeanCloud.Realtime.Internal.Controller { @@ -19,7 +21,7 @@ namespace LeanCloud.Realtime.Internal.Controller { /// /// internal async Task Open(bool force) { - SessionCommand session = NewSessionCommand(); + SessionCommand session = await NewSessionCommand(); session.R = !force; GenericCommand request = NewCommand(CommandType.Session, OpType.Open); request.SessionMessage = session; @@ -32,7 +34,7 @@ namespace LeanCloud.Realtime.Internal.Controller { /// /// internal async Task Reopen() { - SessionCommand session = NewSessionCommand(); + SessionCommand session = await NewSessionCommand(); session.R = true; GenericCommand request = NewCommand(CommandType.Session, OpType.Open); request.SessionMessage = session; @@ -67,14 +69,14 @@ namespace LeanCloud.Realtime.Internal.Controller { #endregion private async Task Refresh() { - SessionCommand session = NewSessionCommand(); + SessionCommand session = await NewSessionCommand(); GenericCommand request = NewCommand(CommandType.Session, OpType.Refresh); request.SessionMessage = session; GenericCommand response = await Client.Connection.SendRequest(request); UpdateSession(response.SessionMessage); } - private SessionCommand NewSessionCommand() { + private async Task NewSessionCommand() { SessionCommand session = new SessionCommand(); if (Client.Tag != null) { session.Tag = Client.Tag; @@ -82,8 +84,21 @@ namespace LeanCloud.Realtime.Internal.Controller { if (Client.DeviceId != null) { session.DeviceId = Client.DeviceId; } + LCIMSignature signature = null; if (Client.SignatureFactory != null) { - LCIMSignature signature = Client.SignatureFactory.CreateConnectSignature(Client.Id); + signature = await Client.SignatureFactory.CreateConnectSignature(Client.Id); + } + if (signature == null && !string.IsNullOrEmpty(Client.SessionToken)) { + Dictionary ret = await LCApplication.HttpClient.Post>("rtm/sign", data: new Dictionary { + { "session_token", Client.SessionToken } + }); + signature = new LCIMSignature { + Signature = ret["signature"] as string, + Timestamp = (long)ret["timestamp"], + Nonce = ret["nonce"] as string + }; + } + if (signature != null) { session.S = signature.Signature; session.T = signature.Timestamp; session.N = signature.Nonce; diff --git a/Realtime/LCIMClient.cs b/Realtime/LCIMClient.cs index be282b3..6e87afd 100644 --- a/Realtime/LCIMClient.cs +++ b/Realtime/LCIMClient.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using System.Linq; using System.Collections.ObjectModel; using LeanCloud.Common; +using LeanCloud.Storage; using LeanCloud.Realtime.Protocol; using LeanCloud.Realtime.Internal.Controller; using LeanCloud.Realtime.Internal.Connection; @@ -27,6 +28,10 @@ namespace LeanCloud.Realtime { get; private set; } + internal string SessionToken { + get; private set; + } + #region 事件 #region 连接状态事件 @@ -248,6 +253,31 @@ namespace LeanCloud.Realtime { string tag = null, string deviceId = null, ILCIMSignatureFactory signatureFactory = null) { + if (string.IsNullOrEmpty(clientId)) { + throw new ArgumentNullException(nameof(clientId)); + } + SetUpClient(clientId, tag, deviceId, signatureFactory); + } + + public LCIMClient(LCUser user, + string tag = null, + string deviceId = null, + ILCIMSignatureFactory signatureFactory = null) { + if (user == null) { + throw new ArgumentNullException(nameof(user)); + } + if (string.IsNullOrEmpty(user.ObjectId) || + string.IsNullOrEmpty(user.SessionToken)) { + throw new ArgumentException("User must be authenticacted."); + } + SetUpClient(user.ObjectId, tag, deviceId, signatureFactory); + SessionToken = user.SessionToken; + } + + private void SetUpClient(string clientId, + string tag, + string deviceId, + ILCIMSignatureFactory signatureFactory) { Id = clientId; Tag = tag; DeviceId = deviceId; diff --git a/Realtime/Signature/ILCIMSignatureFactory.cs b/Realtime/Signature/ILCIMSignatureFactory.cs index 3f4d845..7b62de6 100644 --- a/Realtime/Signature/ILCIMSignatureFactory.cs +++ b/Realtime/Signature/ILCIMSignatureFactory.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Threading.Tasks; namespace LeanCloud.Realtime { public interface ILCIMSignatureFactory { @@ -7,13 +8,13 @@ namespace LeanCloud.Realtime { /// /// /// - LCIMSignature CreateConnectSignature(string clientId); + Task CreateConnectSignature(string clientId); /// /// 创建开启对话签名 /// /// - LCIMSignature CreateStartConversationSignature(string clientId, IEnumerable memberIds); + Task CreateStartConversationSignature(string clientId, IEnumerable memberIds); /// /// 创建会话相关签名 @@ -23,7 +24,7 @@ namespace LeanCloud.Realtime { /// /// /// - LCIMSignature CreateConversationSignature(string conversationId, string clientId, IEnumerable memberIds, string action); + Task CreateConversationSignature(string conversationId, string clientId, IEnumerable memberIds, string action); /// /// 创建黑名单相关签名 @@ -33,6 +34,6 @@ namespace LeanCloud.Realtime { /// /// /// - LCIMSignature CreateBlacklistSignature(string conversationId, string clientId, IEnumerable memberIds, string action); + Task CreateBlacklistSignature(string conversationId, string clientId, IEnumerable memberIds, string action); } } diff --git a/Test/Realtime.Test/Client.cs b/Test/Realtime.Test/Client.cs new file mode 100644 index 0000000..07823d2 --- /dev/null +++ b/Test/Realtime.Test/Client.cs @@ -0,0 +1,7 @@ +using System; +namespace Realtime.Test { + public class Client { + public Client() { + } + } +} diff --git a/Test/Realtime.Test/ConversationQuery.cs b/Test/Realtime.Test/ConversationQuery.cs new file mode 100644 index 0000000..f30a23b --- /dev/null +++ b/Test/Realtime.Test/ConversationQuery.cs @@ -0,0 +1,7 @@ +using System; +namespace Realtime.Test { + public class ConversationQuery { + public ConversationQuery() { + } + } +}