2020-03-12 16:23:21 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Net.Http;
|
2020-03-19 11:04:37 +08:00
|
|
|
|
using LeanCloud.Storage.Internal;
|
2020-03-12 16:23:21 +08:00
|
|
|
|
using LeanCloud.Common;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
namespace LeanCloud.Realtime.Internal.Router {
|
|
|
|
|
internal class LCRTMRouter {
|
|
|
|
|
private LCRTMServer rtmServer;
|
|
|
|
|
|
|
|
|
|
internal LCRTMRouter() {
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-02 18:15:16 +08:00
|
|
|
|
internal async Task<LCRTMServer> GetServer() {
|
2020-03-12 16:23:21 +08:00
|
|
|
|
if (rtmServer == null || !rtmServer.IsValid) {
|
|
|
|
|
await Fetch();
|
|
|
|
|
}
|
2020-04-02 18:15:16 +08:00
|
|
|
|
return rtmServer;
|
2020-03-12 16:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 16:42:30 +08:00
|
|
|
|
internal void Reset() {
|
|
|
|
|
rtmServer = null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 16:23:21 +08:00
|
|
|
|
async Task<LCRTMServer> Fetch() {
|
|
|
|
|
string server = await LCApplication.AppRouter.GetRealtimeServer();
|
|
|
|
|
string url = $"{server}/v1/route?appId={LCApplication.AppId}&secure=1";
|
|
|
|
|
|
|
|
|
|
HttpRequestMessage request = new HttpRequestMessage {
|
|
|
|
|
RequestUri = new Uri(url),
|
|
|
|
|
Method = HttpMethod.Get
|
|
|
|
|
};
|
|
|
|
|
HttpClient client = new HttpClient();
|
|
|
|
|
LCHttpUtils.PrintRequest(client, request);
|
|
|
|
|
HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
|
|
|
|
|
request.Dispose();
|
|
|
|
|
|
|
|
|
|
string resultString = await response.Content.ReadAsStringAsync();
|
|
|
|
|
response.Dispose();
|
|
|
|
|
LCHttpUtils.PrintResponse(response, resultString);
|
|
|
|
|
|
2020-03-19 11:04:37 +08:00
|
|
|
|
rtmServer = JsonConvert.DeserializeObject<LCRTMServer>(resultString, new LCJsonConverter());
|
2020-03-12 16:23:21 +08:00
|
|
|
|
|
|
|
|
|
return rtmServer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|