rename
parent
4c632b49ff
commit
2f58cdfe50
|
@ -5,14 +5,14 @@ using System.Collections.Generic;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace LeanCloud.Common {
|
namespace LeanCloud.Common {
|
||||||
public class AppRouter {
|
public class LCAppRouter {
|
||||||
private readonly string appId;
|
private readonly string appId;
|
||||||
|
|
||||||
private readonly string server;
|
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)) {
|
if (!IsInternalApp(appId) && string.IsNullOrEmpty(server)) {
|
||||||
// 国内节点必须配置自定义域名
|
// 国内节点必须配置自定义域名
|
||||||
throw new Exception("Please init with your server url.");
|
throw new Exception("Please init with your server url.");
|
||||||
|
@ -26,36 +26,49 @@ namespace LeanCloud.Common {
|
||||||
if (!string.IsNullOrEmpty(server)) {
|
if (!string.IsNullOrEmpty(server)) {
|
||||||
return 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)) {
|
if (!IsInternalApp(appId)) {
|
||||||
// 国内节点必须配置自定义域名
|
// 国内节点必须配置自定义域名
|
||||||
throw new Exception("Please init with your server url.");
|
throw new Exception("Please init with your server url.");
|
||||||
}
|
}
|
||||||
// 向 App Router 请求地址
|
// 向 App Router 请求地址
|
||||||
if (appServer == null || appServer.IsExpired) {
|
if (appServer == null || !appServer.IsValid) {
|
||||||
try {
|
try {
|
||||||
HttpRequestMessage request = new HttpRequestMessage {
|
HttpRequestMessage request = new HttpRequestMessage {
|
||||||
RequestUri = new Uri($"https://app-router.com/2/route?appId={appId}"),
|
RequestUri = new Uri($"https://app-router.com/2/route?appId={appId}"),
|
||||||
Method = HttpMethod.Get
|
Method = HttpMethod.Get
|
||||||
};
|
};
|
||||||
HttpClient client = new HttpClient();
|
HttpClient client = new HttpClient();
|
||||||
HttpUtils.PrintRequest(client, request);
|
LCHttpUtils.PrintRequest(client, request);
|
||||||
HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
|
HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
|
||||||
request.Dispose();
|
request.Dispose();
|
||||||
|
|
||||||
string resultString = await response.Content.ReadAsStringAsync();
|
string resultString = await response.Content.ReadAsStringAsync();
|
||||||
response.Dispose();
|
response.Dispose();
|
||||||
HttpUtils.PrintResponse(response, resultString);
|
LCHttpUtils.PrintResponse(response, resultString);
|
||||||
|
|
||||||
Dictionary<string, object> data = JsonConvert.DeserializeObject<Dictionary<string, object>>(resultString);
|
Dictionary<string, object> data = JsonConvert.DeserializeObject<Dictionary<string, object>>(resultString);
|
||||||
appServer = new AppServer(data);
|
appServer = new LCAppServer(data);
|
||||||
} catch (Exception e) {
|
} 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) {
|
private static bool IsInternalApp(string appId) {
|
|
@ -2,7 +2,7 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace LeanCloud.Common {
|
namespace LeanCloud.Common {
|
||||||
public class AppServer {
|
public class LCAppServer {
|
||||||
public string ApiServer {
|
public string ApiServer {
|
||||||
get; private set;
|
get; private set;
|
||||||
}
|
}
|
||||||
|
@ -19,9 +19,9 @@ namespace LeanCloud.Common {
|
||||||
get; private set;
|
get; private set;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsExpired {
|
public bool IsValid {
|
||||||
get {
|
get {
|
||||||
return ttl != -1 && DateTime.Now > expiredAt;
|
return ttl != -1 || DateTime.Now < expiredAt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ namespace LeanCloud.Common {
|
||||||
|
|
||||||
private readonly int ttl;
|
private readonly int ttl;
|
||||||
|
|
||||||
public AppServer(Dictionary<string, object> data) {
|
public LCAppServer(Dictionary<string, object> data) {
|
||||||
ApiServer = GetUrlWithScheme(data["api_server"] as string);
|
ApiServer = GetUrlWithScheme(data["api_server"] as string);
|
||||||
PushServer = GetUrlWithScheme(data["push_server"] as string);
|
PushServer = GetUrlWithScheme(data["push_server"] as string);
|
||||||
EngineServer = GetUrlWithScheme(data["engine_server"] as string);
|
EngineServer = GetUrlWithScheme(data["engine_server"] as string);
|
||||||
|
@ -41,9 +41,9 @@ namespace LeanCloud.Common {
|
||||||
return url.StartsWith("https://") ? url : $"https://{url}";
|
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();
|
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" },
|
{ "api_server", $"https://{prefix}.api.lncldglobal.com" },
|
||||||
{ "push_server", $"https://{prefix}.engine.lncldglobal.com" },
|
{ "push_server", $"https://{prefix}.engine.lncldglobal.com" },
|
||||||
{ "engine_server", $"https://{prefix}.push.lncldglobal.com" },
|
{ "engine_server", $"https://{prefix}.push.lncldglobal.com" },
|
|
@ -3,7 +3,7 @@ using System.Text;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
|
||||||
namespace LeanCloud.Common {
|
namespace LeanCloud.Common {
|
||||||
public static class HttpUtils {
|
public static class LCHttpUtils {
|
||||||
public static void PrintRequest(HttpClient client, HttpRequestMessage request, string content = null) {
|
public static void PrintRequest(HttpClient client, HttpRequestMessage request, string content = null) {
|
||||||
if (client == null) {
|
if (client == null) {
|
||||||
return;
|
return;
|
||||||
|
@ -31,7 +31,7 @@ namespace LeanCloud.Common {
|
||||||
sb.AppendLine($"Content: {content}");
|
sb.AppendLine($"Content: {content}");
|
||||||
}
|
}
|
||||||
sb.AppendLine("=== HTTP Request End ===");
|
sb.AppendLine("=== HTTP Request End ===");
|
||||||
Logger.Debug(sb.ToString());
|
LCLogger.Debug(sb.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void PrintResponse(HttpResponseMessage response, string content = null) {
|
public static void PrintResponse(HttpResponseMessage response, string content = null) {
|
||||||
|
@ -43,7 +43,7 @@ namespace LeanCloud.Common {
|
||||||
sb.AppendLine($"Content: {content}");
|
sb.AppendLine($"Content: {content}");
|
||||||
}
|
}
|
||||||
sb.AppendLine("=== HTTP Response End ===");
|
sb.AppendLine("=== HTTP Response End ===");
|
||||||
Logger.Debug(sb.ToString());
|
LCLogger.Debug(sb.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,7 +5,7 @@ namespace LeanCloud.Common {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 为 Json 解析提供异步接口
|
/// 为 Json 解析提供异步接口
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class JsonUtils {
|
public static class LCJsonUtils {
|
||||||
public static async Task<string> SerializeObjectAsync(object obj) {
|
public static async Task<string> SerializeObjectAsync(object obj) {
|
||||||
string str = null;
|
string str = null;
|
||||||
await Task.Run(() => {
|
await Task.Run(() => {
|
|
@ -2,7 +2,7 @@
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 日志级别
|
/// 日志级别
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public enum LogLevel {
|
public enum LCLogLevel {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 调试级别
|
/// 调试级别
|
||||||
/// </summary>
|
/// </summary>
|
|
@ -4,37 +4,37 @@ namespace LeanCloud.Common {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 日志类
|
/// 日志类
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class Logger {
|
public static class LCLogger {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 日志回调接口,方便开发者调试
|
/// 日志回调接口,方便开发者调试
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The log delegate.</value>
|
/// <value>The log delegate.</value>
|
||||||
public static Action<LogLevel, string> LogDelegate {
|
public static Action<LCLogLevel, string> LogDelegate {
|
||||||
get; set;
|
get; set;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Debug(string log) {
|
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) {
|
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) {
|
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) {
|
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) {
|
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) {
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -10,7 +10,7 @@ namespace LeanCloud.Common {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides helper methods that allow us to use terser code elsewhere.
|
/// Provides helper methods that allow us to use terser code elsewhere.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class TaskExtensions {
|
public static class LCTaskExtensions {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ensures a task (even null) is awaitable.
|
/// Ensures a task (even null) is awaitable.
|
||||||
/// </summary>
|
/// </summary>
|
Loading…
Reference in New Issue