SystemProxy.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. using System.Collections.Concurrent;
  5. using System.Collections;
  6. using UnityEngine.Networking;
  7. public class SystemProxy : ProxyBase<SystemProxy>
  8. {
  9. public SystemProxy() => this.opeCode = OpeCode.ope_system;
  10. /// <summary>
  11. /// [6901]请求系统推送的消息
  12. /// </summary>
  13. /// <param name="ts">时间戳</param>
  14. /// <param name="callback">成功回调(无返回参数)</param>
  15. public void Request(uint ts, Action<SystemProxyRetVo> callback)
  16. {
  17. if (SystemProxy.heartBeatTag)
  18. {
  19. Post<SystemProxyRetVo>(CmdCode.cmd_systemMessage, new object[] { ts }, callback);
  20. }
  21. else
  22. {
  23. callback.Invoke(new SystemProxyRetVo());
  24. }
  25. }
  26. /// <summary>
  27. /// [6902]上报
  28. /// </summary>
  29. /// <param name="ts">时间戳</param>
  30. /// <param name="callback">成功回调(无返回参数)</param>
  31. [Obsolete("功能不完善,封", true)]
  32. public void ReportStatLog(string eventID, string EArgKey, string EArgValue, Action callback, Action onfail)
  33. {
  34. var player = UserProxy.Instance.player;
  35. var uid = player?.uid ?? "stat";
  36. var zoneid = player?.zoneid ?? 1;
  37. Post(CmdCode.cmd_systemStatLogReport, uid, zoneid, new object[] { eventID, EArgKey, EArgValue },
  38. resp => callback?.Invoke(),
  39. o => onfail?.Invoke()
  40. );
  41. }
  42. /// <summary>
  43. /// [6903]客户端心跳包
  44. /// </summary>
  45. /// <param name="callback"></param>
  46. public void Heartbeat(Action<int> callback)
  47. {
  48. var ts = UserProxy.Instance.GetCurrentUnixTimeStamp();
  49. Post(CmdCode.cmd_sysemHeartbeat, new object[] { }, resp =>
  50. {
  51. var delay = UserProxy.Instance.GetCurrentUnixTimeStamp() - ts;
  52. callback?.Invoke((int)delay);
  53. });
  54. }
  55. public static bool heartBeatTag = false;
  56. /// <summary>
  57. /// 心跳包引擎
  58. /// </summary>
  59. public class HearbeatBehavor : MonoSingleton<HearbeatBehavor>
  60. {
  61. private float ts = 0;
  62. private void FixedUpdate()
  63. {
  64. ts += Time.deltaTime;
  65. if (ts > 30)
  66. {
  67. ts = 0;
  68. if (heartBeatTag)
  69. {
  70. SystemProxy.Instance.Heartbeat(delay =>
  71. {
  72. if (delay > 0)
  73. {
  74. LogHelper.LogWarning($"通讯延时大概{delay}秒");
  75. }
  76. });
  77. }
  78. }
  79. }
  80. public void Start() { } //
  81. }
  82. }
  83. /// <summary>
  84. /// 统计辅助类
  85. /// </summary>
  86. class StatHelper : MonoSingleton<StatHelper>
  87. {
  88. class EventLog
  89. {
  90. public string EventID;
  91. public string EArgKey;
  92. public string EArgValue;
  93. public string UID = "-";
  94. public string zoneid = "0";
  95. }
  96. // private Dictionary<string,KeyValuePair<string,string>> statLogs = new Dictionary<string, KeyValuePair<string, string>>();
  97. private ConcurrentQueue<EventLog> statLogs = new ConcurrentQueue<EventLog>();
  98. /// <summary>
  99. /// 添加自定义事件
  100. /// </summary>
  101. /// <param name="eventID">事件ID</param>
  102. /// <param name="EArgKey">事件参数-key</param>
  103. /// <param name="EArgValue">事件参数-value</param>
  104. public void AddEvent(string eventID, string EArgKey, string EArgValue)
  105. {
  106. var e = new EventLog() { EventID = eventID, EArgKey = EArgKey, EArgValue = EArgValue };
  107. if (null != UserProxy.Instance.player)
  108. {
  109. e.UID = UserProxy.Instance.player.uid;
  110. e.zoneid = UserProxy.Instance.player.zoneid.ToString();
  111. }
  112. statLogs.Enqueue(e);
  113. }
  114. private static int retryTimes = 0;
  115. private void Update()
  116. {
  117. if (statLogs.TryDequeue(out var item) && retryTimes < 3)
  118. {
  119. var url = Config_URL.Server_URL + "service_call/InquireApi/ReportStatLog.php";
  120. var li = new List<KeyValuePair<string, string>>();
  121. li.Add(new KeyValuePair<string, string>(nameof(item.EventID), item.EventID));
  122. li.Add(new KeyValuePair<string, string>(nameof(item.EArgKey), item.EArgKey));
  123. li.Add(new KeyValuePair<string, string>(nameof(item.EArgValue), item.EArgValue));
  124. li.Add(new KeyValuePair<string, string>(nameof(item.UID), item.UID));
  125. li.Add(new KeyValuePair<string, string>(nameof(item.zoneid), item.zoneid));
  126. HttpHelper.Instance.Post(url, li,
  127. str =>
  128. { // 成功
  129. },
  130. err =>
  131. { // 失败
  132. retryTimes++;
  133. statLogs.Enqueue(item); // 再重新发一次好了
  134. });
  135. }
  136. }
  137. /// <summary>
  138. /// HTTP辅助类
  139. /// </summary>
  140. public class HttpHelper : MonoSingleton<HttpHelper>
  141. {
  142. public void Get(string url, Action<string> OnResult, Action<string> OnErr)
  143. {
  144. StartCoroutine(GetRequest(url, OnResult, OnErr));
  145. }
  146. public void Post(string url, List<KeyValuePair<string, string>> paras, Action<string> OnResult, Action<string> OnErr)
  147. {
  148. // CoroutineWrapper.Instance.EXEDelayFrames();
  149. StartCoroutine(PostRequest(url, paras, OnResult, OnErr));
  150. }
  151. #region ' 网络下载功能 '
  152. // todo: 这里考虑增设cache设计,扩展下载和缓存功能.
  153. // 对于这种资源没必要每次都完整下载,这样多浪费啊,都是钱啊.
  154. // -by gwang 2017年1月20日 16:41:17
  155. // 经验证: 在WWW中加入header if-modified-since字段可以得倒304(Nginx服务器,资源为txt类型)
  156. // 后续需要做的是一个文件管理系统,管理本地缓存文件的时间戳.
  157. // 当调用下载的时候,先检查缓存中的文件,拿到本地时间戳(FileInfo.LastWriteTimeUtc.ToString("r")),然后请求服务器,
  158. // 这样,虽然每次都请求了,但是不必每次都下发完整的文件,如果没有更新的情况下,只需返回304即可.
  159. #endregion
  160. //
  161. #region 私有方法
  162. IEnumerator GetRequest(string url, Action<string> OnResult, Action<string> OnErr)
  163. {
  164. using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
  165. {
  166. yield return webRequest.SendWebRequest();
  167. if (!string.IsNullOrEmpty(webRequest.error))
  168. {
  169. OnErr?.Invoke(webRequest.error);
  170. }
  171. else
  172. {
  173. OnResult?.Invoke(webRequest.downloadHandler.text);
  174. }
  175. }
  176. }
  177. ///
  178. IEnumerator PostRequest(string url, List<KeyValuePair<string, string>> paras, Action<string> OnResult, Action<string> OnErr)
  179. {
  180. WWWForm form = new WWWForm();
  181. paras.ForEach(kv => form.AddField(kv.Key, kv.Value)); // 压入参数
  182. using (UnityWebRequest webRequest = UnityWebRequest.Post(url, form))
  183. {
  184. yield return webRequest.SendWebRequest();
  185. if (!string.IsNullOrEmpty(webRequest.error))
  186. {
  187. OnErr?.Invoke(webRequest.error);
  188. }
  189. else
  190. {
  191. OnResult?.Invoke(webRequest.downloadHandler.text);
  192. }
  193. }
  194. }
  195. #endregion
  196. }
  197. }