csharp-sdk-upm/Common/AppRouter/LCAppServer.cs

55 lines
1.6 KiB
C#
Raw Normal View History

2020-03-09 12:31:25 +08:00
using System;
using System.Collections.Generic;
namespace LeanCloud.Common {
2020-03-10 16:54:50 +08:00
public class LCAppServer {
2020-03-09 12:31:25 +08:00
public string ApiServer {
get; private set;
}
public string EngineServer {
get; private set;
}
public string PushServer {
get; private set;
}
public string RTMServer {
get; private set;
}
2020-03-10 16:54:50 +08:00
public bool IsValid {
2020-03-09 12:31:25 +08:00
get {
2020-03-10 16:54:50 +08:00
return ttl != -1 || DateTime.Now < expiredAt;
2020-03-09 12:31:25 +08:00
}
}
private readonly DateTime expiredAt;
private readonly int ttl;
2020-03-10 16:54:50 +08:00
public LCAppServer(Dictionary<string, object> data) {
2020-03-09 12:31:25 +08:00
ApiServer = GetUrlWithScheme(data["api_server"] as string);
PushServer = GetUrlWithScheme(data["push_server"] as string);
EngineServer = GetUrlWithScheme(data["engine_server"] as string);
ttl = (int)(long)data["ttl"];
expiredAt = DateTime.Now.AddSeconds(ttl);
}
private static string GetUrlWithScheme(string url) {
return url.StartsWith("https://") ? url : $"https://{url}";
}
2020-03-10 16:54:50 +08:00
internal static LCAppServer GetInternalFallbackAppServer(string appId) {
2020-03-09 12:31:25 +08:00
string prefix = appId.Substring(0, 8).ToLower();
2020-03-10 16:54:50 +08:00
return new LCAppServer(new Dictionary<string, object> {
2020-03-09 12:31:25 +08:00
{ "api_server", $"https://{prefix}.api.lncldglobal.com" },
{ "push_server", $"https://{prefix}.engine.lncldglobal.com" },
{ "engine_server", $"https://{prefix}.push.lncldglobal.com" },
{ "ttl", -1 }
});
}
}
}