163 lines
5.5 KiB
C#
163 lines
5.5 KiB
C#
using asap.core;
|
|
using LitJson;
|
|
using System;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
public interface ICustomServerMgr
|
|
{
|
|
Task<string> GetStringAsync(string url);
|
|
Task<string> CustomServerPost(string url, string bodyString, string contentType = "application/json");
|
|
Task<string> AccountRequest(string url, object data);
|
|
Task<string> EventRequest(string url, object data);
|
|
Task<string> ClubRequest(string url, object data);
|
|
Task<string> BombRequest(string url, object data);
|
|
Task<string> DuelRequest(string url, object data);
|
|
Task<string> CommonDataRequest(string url, object data);
|
|
Task<string> FriendRequest(string url, object data);
|
|
Task<T> EventPartnerRequest<T>(string url, object data = null);
|
|
CookieContainer Cookie { get; }
|
|
}
|
|
|
|
public class CustomServerMgr : ICustomServerMgr
|
|
{
|
|
IConfig config { get; set; }
|
|
|
|
//网络客户端
|
|
HttpClient client;
|
|
|
|
private CookieContainer _cookie;
|
|
public CookieContainer Cookie => _cookie ?? throw new System.NullReferenceException();
|
|
|
|
public CustomServerMgr(IConfig config)
|
|
{
|
|
this.config = config;
|
|
|
|
var httpClientHandler = new HttpClientHandler();
|
|
_cookie = new CookieContainer();
|
|
httpClientHandler.CookieContainer = _cookie;
|
|
client = new HttpClient(httpClientHandler);
|
|
|
|
client.BaseAddress =
|
|
new System.Uri(config.Get<string>(GConstant.K_Event_API_URL,
|
|
"http://192.168.9.101:5292/"));
|
|
client.Timeout = TimeSpan.FromSeconds(15);
|
|
}
|
|
|
|
public async Task<string> CustomServerPost(string url, string bodyString,
|
|
string contentType = "application/json")
|
|
{
|
|
try
|
|
{
|
|
//#if UNITY_EDITOR
|
|
// Debug.Log("CustomServerPost url:" + url);
|
|
// Debug.Log("CustomServerPost bodyString:" + bodyString);
|
|
//#endif
|
|
var content = new StringContent(bodyString, Encoding.UTF8, "application/json");
|
|
var response = await client.PostAsync(url, content);
|
|
string result = await response.Content.ReadAsStringAsync();
|
|
if (response.StatusCode == System.Net.HttpStatusCode.OK)
|
|
{
|
|
//GameDebug.Log("CustomServerPost result:" + result.ToString());
|
|
return result;
|
|
}
|
|
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized || response.StatusCode == System.Net.HttpStatusCode.Forbidden)
|
|
{
|
|
GContext.Publish(new game.EvtAPISvrUnauthorized());
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError(response.StatusCode.ToString() + "+++CustomServerPost error:" +
|
|
result.ToString() + "+++CustomServerPost url:" + url + ":" +
|
|
bodyString);
|
|
return null;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError("CustomServerPost error:" + e.Message + "---CustomServerPost url:" +
|
|
url + ":" + bodyString + "\n" + e.StackTrace);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public async Task<string> GetStringAsync(string url)
|
|
{
|
|
var response = await client.GetAsync(url);
|
|
|
|
if (response.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
return await response.Content.ReadAsStringAsync();
|
|
}
|
|
|
|
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
|
{
|
|
GContext.Publish(new game.EvtAPISvrUnauthorized());
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public async Task<string> AccountRequest(string url, object data)
|
|
{
|
|
return await CustomServerPost($"account/{url}",
|
|
Newtonsoft.Json.JsonConvert.SerializeObject(data));
|
|
}
|
|
|
|
public async Task<string> EventRequest(string url, object data)
|
|
{
|
|
return await CustomServerPost($"event/{url}",
|
|
Newtonsoft.Json.JsonConvert.SerializeObject(data));
|
|
}
|
|
|
|
public async Task<string> ClubRequest(string url, object data)
|
|
{
|
|
return await CustomServerPost($"club/{url}",
|
|
Newtonsoft.Json.JsonConvert.SerializeObject(data));
|
|
}
|
|
|
|
public async Task<string> BombRequest(string url, object data)
|
|
{
|
|
return await CustomServerPost($"bomb/{url}",
|
|
Newtonsoft.Json.JsonConvert.SerializeObject(data));
|
|
}
|
|
|
|
public async Task<string> CommonDataRequest(string url, object data)
|
|
{
|
|
return await CustomServerPost($"commondata/{url}",
|
|
Newtonsoft.Json.JsonConvert.SerializeObject(data));
|
|
}
|
|
|
|
public async Task<string> DuelRequest(string url, object data)
|
|
{
|
|
return await CustomServerPost($"duel/{url}",
|
|
Newtonsoft.Json.JsonConvert.SerializeObject(data));
|
|
}
|
|
|
|
public async Task<string> FriendRequest(string url, object data)
|
|
{
|
|
return await CustomServerPost($"friend/{url}",
|
|
Newtonsoft.Json.JsonConvert.SerializeObject(data));
|
|
}
|
|
|
|
public async Task<T> EventPartnerRequest<T>(string url, object data = null)
|
|
{
|
|
var body = Newtonsoft.Json.JsonConvert.SerializeObject(data);
|
|
var responseString = await CustomServerPost($"eventpartner/{url}", body);
|
|
try
|
|
{
|
|
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(responseString);
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
Debug.LogError(
|
|
$"<color=red>[EventPartner] Response {responseString} failed to be deserialized: {e.Message}</color>");
|
|
return default(T);
|
|
}
|
|
}
|
|
}
|