using System;
using System.Threading.Tasks;
using System.Net.Http;
using LeanCloud.Storage.Internal;
using LeanCloud.Common;
using Newtonsoft.Json;
namespace LeanCloud.Realtime.Internal.Router {
///
/// RTM Router
///
internal class LCRTMRouter {
///
/// 请求超时
///
private const int REQUEST_TIMEOUT = 10000;
private LCRTMServer rtmServer;
internal LCRTMRouter() {
}
///
/// 获取服务器地址
///
///
internal async Task GetServer() {
if (rtmServer == null || !rtmServer.IsValid) {
await Fetch();
}
return rtmServer;
}
async Task 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);
Task requestTask = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
if (await Task.WhenAny(requestTask, Task.Delay(REQUEST_TIMEOUT)) != requestTask) {
throw new TimeoutException("Request timeout.");
}
HttpResponseMessage response = await requestTask;
request.Dispose();
string resultString = await response.Content.ReadAsStringAsync();
response.Dispose();
LCHttpUtils.PrintResponse(response, resultString);
rtmServer = JsonConvert.DeserializeObject(resultString, new LCJsonConverter());
return rtmServer;
}
}
}