* LCIMClient.cs:

* LCIMSessionController.cs:
* LCIMConversationController.cs:

* ILCIMSignatureFactory.cs: chore: 支持 LCUser 登录
oneRain 2020-04-23 12:07:30 +08:00
parent 0ea13ab725
commit acee284f67
6 changed files with 74 additions and 14 deletions

View File

@ -57,7 +57,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
}; };
} }
if (Client.SignatureFactory != null) { 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.S = signature.Signature;
conv.T = signature.Timestamp; conv.T = signature.Timestamp;
conv.N = signature.Nonce; conv.N = signature.Nonce;
@ -145,7 +145,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
conv.M.AddRange(clientIds); conv.M.AddRange(clientIds);
// 签名参数 // 签名参数
if (Client.SignatureFactory != null) { if (Client.SignatureFactory != null) {
LCIMSignature signature = Client.SignatureFactory.CreateConversationSignature(convId, LCIMSignature signature = await Client.SignatureFactory.CreateConversationSignature(convId,
Client.Id, Client.Id,
clientIds, clientIds,
LCIMSignatureAction.Invite); LCIMSignatureAction.Invite);
@ -175,7 +175,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
conv.M.AddRange(removeIds); conv.M.AddRange(removeIds);
// 签名参数 // 签名参数
if (Client.SignatureFactory != null) { if (Client.SignatureFactory != null) {
LCIMSignature signature = Client.SignatureFactory.CreateConversationSignature(convId, LCIMSignature signature = await Client.SignatureFactory.CreateConversationSignature(convId,
Client.Id, Client.Id,
removeIds, removeIds,
LCIMSignatureAction.Kick); LCIMSignatureAction.Kick);
@ -271,7 +271,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
}; };
blacklist.ToPids.AddRange(clientIds); blacklist.ToPids.AddRange(clientIds);
if (Client.SignatureFactory != null) { if (Client.SignatureFactory != null) {
LCIMSignature signature = Client.SignatureFactory.CreateBlacklistSignature(convId, LCIMSignature signature = await Client.SignatureFactory.CreateBlacklistSignature(convId,
Client.Id, Client.Id,
clientIds, clientIds,
LCIMSignatureAction.ConversationBlockClients); LCIMSignatureAction.ConversationBlockClients);
@ -298,7 +298,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
}; };
blacklist.ToPids.AddRange(clientIds); blacklist.ToPids.AddRange(clientIds);
if (Client.SignatureFactory != null) { if (Client.SignatureFactory != null) {
LCIMSignature signature = Client.SignatureFactory.CreateBlacklistSignature(convId, LCIMSignature signature = await Client.SignatureFactory.CreateBlacklistSignature(convId,
Client.Id, Client.Id,
clientIds, clientIds,
LCIMSignatureAction.ConversationUnblockClients); LCIMSignatureAction.ConversationUnblockClients);

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using LeanCloud.Storage;
using LeanCloud.Realtime.Protocol; using LeanCloud.Realtime.Protocol;
namespace LeanCloud.Realtime.Internal.Controller { namespace LeanCloud.Realtime.Internal.Controller {
@ -19,7 +21,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
internal async Task Open(bool force) { internal async Task Open(bool force) {
SessionCommand session = NewSessionCommand(); SessionCommand session = await NewSessionCommand();
session.R = !force; session.R = !force;
GenericCommand request = NewCommand(CommandType.Session, OpType.Open); GenericCommand request = NewCommand(CommandType.Session, OpType.Open);
request.SessionMessage = session; request.SessionMessage = session;
@ -32,7 +34,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
internal async Task Reopen() { internal async Task Reopen() {
SessionCommand session = NewSessionCommand(); SessionCommand session = await NewSessionCommand();
session.R = true; session.R = true;
GenericCommand request = NewCommand(CommandType.Session, OpType.Open); GenericCommand request = NewCommand(CommandType.Session, OpType.Open);
request.SessionMessage = session; request.SessionMessage = session;
@ -67,14 +69,14 @@ namespace LeanCloud.Realtime.Internal.Controller {
#endregion #endregion
private async Task Refresh() { private async Task Refresh() {
SessionCommand session = NewSessionCommand(); SessionCommand session = await NewSessionCommand();
GenericCommand request = NewCommand(CommandType.Session, OpType.Refresh); GenericCommand request = NewCommand(CommandType.Session, OpType.Refresh);
request.SessionMessage = session; request.SessionMessage = session;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Client.Connection.SendRequest(request);
UpdateSession(response.SessionMessage); UpdateSession(response.SessionMessage);
} }
private SessionCommand NewSessionCommand() { private async Task<SessionCommand> NewSessionCommand() {
SessionCommand session = new SessionCommand(); SessionCommand session = new SessionCommand();
if (Client.Tag != null) { if (Client.Tag != null) {
session.Tag = Client.Tag; session.Tag = Client.Tag;
@ -82,8 +84,21 @@ namespace LeanCloud.Realtime.Internal.Controller {
if (Client.DeviceId != null) { if (Client.DeviceId != null) {
session.DeviceId = Client.DeviceId; session.DeviceId = Client.DeviceId;
} }
LCIMSignature signature = null;
if (Client.SignatureFactory != 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<string, object> ret = await LCApplication.HttpClient.Post<Dictionary<string, object>>("rtm/sign", data: new Dictionary<string, object> {
{ "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.S = signature.Signature;
session.T = signature.Timestamp; session.T = signature.Timestamp;
session.N = signature.Nonce; session.N = signature.Nonce;

View File

@ -4,6 +4,7 @@ using System.Threading.Tasks;
using System.Linq; using System.Linq;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using LeanCloud.Common; using LeanCloud.Common;
using LeanCloud.Storage;
using LeanCloud.Realtime.Protocol; using LeanCloud.Realtime.Protocol;
using LeanCloud.Realtime.Internal.Controller; using LeanCloud.Realtime.Internal.Controller;
using LeanCloud.Realtime.Internal.Connection; using LeanCloud.Realtime.Internal.Connection;
@ -27,6 +28,10 @@ namespace LeanCloud.Realtime {
get; private set; get; private set;
} }
internal string SessionToken {
get; private set;
}
#region 事件 #region 事件
#region 连接状态事件 #region 连接状态事件
@ -248,6 +253,31 @@ namespace LeanCloud.Realtime {
string tag = null, string tag = null,
string deviceId = null, string deviceId = null,
ILCIMSignatureFactory signatureFactory = 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; Id = clientId;
Tag = tag; Tag = tag;
DeviceId = deviceId; DeviceId = deviceId;

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
namespace LeanCloud.Realtime { namespace LeanCloud.Realtime {
public interface ILCIMSignatureFactory { public interface ILCIMSignatureFactory {
@ -7,13 +8,13 @@ namespace LeanCloud.Realtime {
/// </summary> /// </summary>
/// <param name="clientId"></param> /// <param name="clientId"></param>
/// <returns></returns> /// <returns></returns>
LCIMSignature CreateConnectSignature(string clientId); Task<LCIMSignature> CreateConnectSignature(string clientId);
/// <summary> /// <summary>
/// 创建开启对话签名 /// 创建开启对话签名
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
LCIMSignature CreateStartConversationSignature(string clientId, IEnumerable<string> memberIds); Task<LCIMSignature> CreateStartConversationSignature(string clientId, IEnumerable<string> memberIds);
/// <summary> /// <summary>
/// 创建会话相关签名 /// 创建会话相关签名
@ -23,7 +24,7 @@ namespace LeanCloud.Realtime {
/// <param name="memberIds"></param> /// <param name="memberIds"></param>
/// <param name="action"></param> /// <param name="action"></param>
/// <returns></returns> /// <returns></returns>
LCIMSignature CreateConversationSignature(string conversationId, string clientId, IEnumerable<string> memberIds, string action); Task<LCIMSignature> CreateConversationSignature(string conversationId, string clientId, IEnumerable<string> memberIds, string action);
/// <summary> /// <summary>
/// 创建黑名单相关签名 /// 创建黑名单相关签名
@ -33,6 +34,6 @@ namespace LeanCloud.Realtime {
/// <param name="memberIds"></param> /// <param name="memberIds"></param>
/// <param name="action"></param> /// <param name="action"></param>
/// <returns></returns> /// <returns></returns>
LCIMSignature CreateBlacklistSignature(string conversationId, string clientId, IEnumerable<string> memberIds, string action); Task<LCIMSignature> CreateBlacklistSignature(string conversationId, string clientId, IEnumerable<string> memberIds, string action);
} }
} }

View File

@ -0,0 +1,7 @@
using System;
namespace Realtime.Test {
public class Client {
public Client() {
}
}
}

View File

@ -0,0 +1,7 @@
using System;
namespace Realtime.Test {
public class ConversationQuery {
public ConversationQuery() {
}
}
}