45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
|
using System;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Net.Http;
|
|||
|
using LeanCloud;
|
|||
|
using LeanCloud.Common;
|
|||
|
using Newtonsoft.Json;
|
|||
|
|
|||
|
namespace LeanCloud.Realtime.Internal.Router {
|
|||
|
internal class LCRTMRouter {
|
|||
|
private LCRTMServer rtmServer;
|
|||
|
|
|||
|
internal LCRTMRouter() {
|
|||
|
}
|
|||
|
|
|||
|
internal async Task<string> GetServer() {
|
|||
|
if (rtmServer == null || !rtmServer.IsValid) {
|
|||
|
await Fetch();
|
|||
|
}
|
|||
|
return rtmServer.Server;
|
|||
|
}
|
|||
|
|
|||
|
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);
|
|||
|
|
|||
|
rtmServer = JsonConvert.DeserializeObject<LCRTMServer>(resultString);
|
|||
|
|
|||
|
return rtmServer;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|