oneRain 2020-03-10 16:54:50 +08:00
parent 4c632b49ff
commit 2f58cdfe50
7 changed files with 43 additions and 30 deletions

View File

@ -5,14 +5,14 @@ using System.Collections.Generic;
using Newtonsoft.Json;
namespace LeanCloud.Common {
public class AppRouter {
public class LCAppRouter {
private readonly string appId;
private readonly string server;
private AppServer appServer;
private LCAppServer appServer;
public AppRouter(string appId, string server) {
public LCAppRouter(string appId, string server) {
if (!IsInternalApp(appId) && string.IsNullOrEmpty(server)) {
// 国内节点必须配置自定义域名
throw new Exception("Please init with your server url.");
@ -26,36 +26,49 @@ namespace LeanCloud.Common {
if (!string.IsNullOrEmpty(server)) {
return server;
}
LCAppServer appServ = await FetchAppServer();
return appServ.ApiServer;
}
public async Task<string> GetRealtimeServer() {
if (!string.IsNullOrEmpty(server)) {
return server;
}
LCAppServer appServ = await FetchAppServer();
return appServ.PushServer;
}
async Task<LCAppServer> FetchAppServer() {
// 判断节点地区
if (!IsInternalApp(appId)) {
// 国内节点必须配置自定义域名
throw new Exception("Please init with your server url.");
}
// 向 App Router 请求地址
if (appServer == null || appServer.IsExpired) {
if (appServer == null || !appServer.IsValid) {
try {
HttpRequestMessage request = new HttpRequestMessage {
RequestUri = new Uri($"https://app-router.com/2/route?appId={appId}"),
Method = HttpMethod.Get
};
HttpClient client = new HttpClient();
HttpUtils.PrintRequest(client, request);
LCHttpUtils.PrintRequest(client, request);
HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
request.Dispose();
string resultString = await response.Content.ReadAsStringAsync();
response.Dispose();
HttpUtils.PrintResponse(response, resultString);
LCHttpUtils.PrintResponse(response, resultString);
Dictionary<string, object> data = JsonConvert.DeserializeObject<Dictionary<string, object>>(resultString);
appServer = new AppServer(data);
appServer = new LCAppServer(data);
} catch (Exception e) {
Logger.Error(e.Message);
LCLogger.Error(e.Message);
// 拉取服务地址失败后,使用国际节点的默认服务地址
appServer = AppServer.GetInternalFallbackAppServer(appId);
appServer = LCAppServer.GetInternalFallbackAppServer(appId);
}
}
return appServer.ApiServer;
return appServer;
}
private static bool IsInternalApp(string appId) {

View File

@ -2,7 +2,7 @@
using System.Collections.Generic;
namespace LeanCloud.Common {
public class AppServer {
public class LCAppServer {
public string ApiServer {
get; private set;
}
@ -19,9 +19,9 @@ namespace LeanCloud.Common {
get; private set;
}
public bool IsExpired {
public bool IsValid {
get {
return ttl != -1 && DateTime.Now > expiredAt;
return ttl != -1 || DateTime.Now < expiredAt;
}
}
@ -29,7 +29,7 @@ namespace LeanCloud.Common {
private readonly int ttl;
public AppServer(Dictionary<string, object> data) {
public LCAppServer(Dictionary<string, object> data) {
ApiServer = GetUrlWithScheme(data["api_server"] as string);
PushServer = GetUrlWithScheme(data["push_server"] as string);
EngineServer = GetUrlWithScheme(data["engine_server"] as string);
@ -41,9 +41,9 @@ namespace LeanCloud.Common {
return url.StartsWith("https://") ? url : $"https://{url}";
}
internal static AppServer GetInternalFallbackAppServer(string appId) {
internal static LCAppServer GetInternalFallbackAppServer(string appId) {
string prefix = appId.Substring(0, 8).ToLower();
return new AppServer(new Dictionary<string, object> {
return new LCAppServer(new Dictionary<string, object> {
{ "api_server", $"https://{prefix}.api.lncldglobal.com" },
{ "push_server", $"https://{prefix}.engine.lncldglobal.com" },
{ "engine_server", $"https://{prefix}.push.lncldglobal.com" },

View File

@ -3,7 +3,7 @@ using System.Text;
using System.Net.Http;
namespace LeanCloud.Common {
public static class HttpUtils {
public static class LCHttpUtils {
public static void PrintRequest(HttpClient client, HttpRequestMessage request, string content = null) {
if (client == null) {
return;
@ -31,7 +31,7 @@ namespace LeanCloud.Common {
sb.AppendLine($"Content: {content}");
}
sb.AppendLine("=== HTTP Request End ===");
Logger.Debug(sb.ToString());
LCLogger.Debug(sb.ToString());
}
public static void PrintResponse(HttpResponseMessage response, string content = null) {
@ -43,7 +43,7 @@ namespace LeanCloud.Common {
sb.AppendLine($"Content: {content}");
}
sb.AppendLine("=== HTTP Response End ===");
Logger.Debug(sb.ToString());
LCLogger.Debug(sb.ToString());
}
}
}

View File

@ -5,7 +5,7 @@ namespace LeanCloud.Common {
/// <summary>
/// 为 Json 解析提供异步接口
/// </summary>
public static class JsonUtils {
public static class LCJsonUtils {
public static async Task<string> SerializeObjectAsync(object obj) {
string str = null;
await Task.Run(() => {

View File

@ -2,7 +2,7 @@
/// <summary>
/// 日志级别
/// </summary>
public enum LogLevel {
public enum LCLogLevel {
/// <summary>
/// 调试级别
/// </summary>

View File

@ -4,37 +4,37 @@ namespace LeanCloud.Common {
/// <summary>
/// 日志类
/// </summary>
public static class Logger {
public static class LCLogger {
/// <summary>
/// 日志回调接口,方便开发者调试
/// </summary>
/// <value>The log delegate.</value>
public static Action<LogLevel, string> LogDelegate {
public static Action<LCLogLevel, string> LogDelegate {
get; set;
}
public static void Debug(string log) {
LogDelegate?.Invoke(LogLevel.Debug, log);
LogDelegate?.Invoke(LCLogLevel.Debug, log);
}
public static void Debug(string format, params object[] args) {
LogDelegate?.Invoke(LogLevel.Debug, string.Format(format, args));
LogDelegate?.Invoke(LCLogLevel.Debug, string.Format(format, args));
}
public static void Warn(string log) {
LogDelegate?.Invoke(LogLevel.Warn, log);
LogDelegate?.Invoke(LCLogLevel.Warn, log);
}
public static void Warn(string format, params object[] args) {
LogDelegate?.Invoke(LogLevel.Warn, string.Format(format, args));
LogDelegate?.Invoke(LCLogLevel.Warn, string.Format(format, args));
}
public static void Error(string log) {
LogDelegate?.Invoke(LogLevel.Error, log);
LogDelegate?.Invoke(LCLogLevel.Error, log);
}
public static void Error(string format, params object[] args) {
LogDelegate?.Invoke(LogLevel.Error, string.Format(format, args));
LogDelegate?.Invoke(LCLogLevel.Error, string.Format(format, args));
}
}
}

View File

@ -10,7 +10,7 @@ namespace LeanCloud.Common {
/// <summary>
/// Provides helper methods that allow us to use terser code elsewhere.
/// </summary>
public static class TaskExtensions {
public static class LCTaskExtensions {
/// <summary>
/// Ensures a task (even null) is awaitable.
/// </summary>