123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- using System;
- using UnityEngine;
- using System.Collections.Generic;
- using System.Collections.Concurrent;
- using System.Collections;
- using UnityEngine.Networking;
- public class SystemProxy : ProxyBase<SystemProxy>
- {
- public SystemProxy() => this.opeCode = OpeCode.ope_system;
- /// <summary>
- /// [6901]请求系统推送的消息
- /// </summary>
- /// <param name="ts">时间戳</param>
- /// <param name="callback">成功回调(无返回参数)</param>
- public void Request(uint ts, Action<SystemProxyRetVo> callback)
- {
- if (SystemProxy.heartBeatTag)
- {
- Post<SystemProxyRetVo>(CmdCode.cmd_systemMessage, new object[] { ts }, callback);
- }
- else
- {
- callback.Invoke(new SystemProxyRetVo());
- }
- }
- /// <summary>
- /// [6902]上报
- /// </summary>
- /// <param name="ts">时间戳</param>
- /// <param name="callback">成功回调(无返回参数)</param>
- [Obsolete("功能不完善,封", true)]
- public void ReportStatLog(string eventID, string EArgKey, string EArgValue, Action callback, Action onfail)
- {
- var player = UserProxy.Instance.player;
- var uid = player?.uid ?? "stat";
- var zoneid = player?.zoneid ?? 1;
- Post(CmdCode.cmd_systemStatLogReport, uid, zoneid, new object[] { eventID, EArgKey, EArgValue },
- resp => callback?.Invoke(),
- o => onfail?.Invoke()
- );
- }
- /// <summary>
- /// [6903]客户端心跳包
- /// </summary>
- /// <param name="callback"></param>
- public void Heartbeat(Action<int> callback)
- {
- var ts = UserProxy.Instance.GetCurrentUnixTimeStamp();
- Post(CmdCode.cmd_sysemHeartbeat, new object[] { }, resp =>
- {
- var delay = UserProxy.Instance.GetCurrentUnixTimeStamp() - ts;
- callback?.Invoke((int)delay);
- });
- }
- public static bool heartBeatTag = false;
- /// <summary>
- /// 心跳包引擎
- /// </summary>
- public class HearbeatBehavor : MonoSingleton<HearbeatBehavor>
- {
- private float ts = 0;
- private void FixedUpdate()
- {
- ts += Time.deltaTime;
- if (ts > 30)
- {
- ts = 0;
- if (heartBeatTag)
- {
- SystemProxy.Instance.Heartbeat(delay =>
- {
- if (delay > 0)
- {
- LogHelper.LogWarning($"通讯延时大概{delay}秒");
- }
- });
- }
- }
- }
- public void Start() { } //
- }
- }
- /// <summary>
- /// 统计辅助类
- /// </summary>
- class StatHelper : MonoSingleton<StatHelper>
- {
- class EventLog
- {
- public string EventID;
- public string EArgKey;
- public string EArgValue;
- public string UID = "-";
- public string zoneid = "0";
- }
- // private Dictionary<string,KeyValuePair<string,string>> statLogs = new Dictionary<string, KeyValuePair<string, string>>();
- private ConcurrentQueue<EventLog> statLogs = new ConcurrentQueue<EventLog>();
- /// <summary>
- /// 添加自定义事件
- /// </summary>
- /// <param name="eventID">事件ID</param>
- /// <param name="EArgKey">事件参数-key</param>
- /// <param name="EArgValue">事件参数-value</param>
- public void AddEvent(string eventID, string EArgKey, string EArgValue)
- {
- var e = new EventLog() { EventID = eventID, EArgKey = EArgKey, EArgValue = EArgValue };
- if (null != UserProxy.Instance.player)
- {
- e.UID = UserProxy.Instance.player.uid;
- e.zoneid = UserProxy.Instance.player.zoneid.ToString();
- }
- statLogs.Enqueue(e);
- }
- private static int retryTimes = 0;
- private void Update()
- {
- if (statLogs.TryDequeue(out var item) && retryTimes < 3)
- {
- var url = Config_URL.Server_URL + "service_call/InquireApi/ReportStatLog.php";
- var li = new List<KeyValuePair<string, string>>();
- li.Add(new KeyValuePair<string, string>(nameof(item.EventID), item.EventID));
- li.Add(new KeyValuePair<string, string>(nameof(item.EArgKey), item.EArgKey));
- li.Add(new KeyValuePair<string, string>(nameof(item.EArgValue), item.EArgValue));
- li.Add(new KeyValuePair<string, string>(nameof(item.UID), item.UID));
- li.Add(new KeyValuePair<string, string>(nameof(item.zoneid), item.zoneid));
- HttpHelper.Instance.Post(url, li,
- str =>
- { // 成功
- },
- err =>
- { // 失败
- retryTimes++;
- statLogs.Enqueue(item); // 再重新发一次好了
- });
- }
- }
- /// <summary>
- /// HTTP辅助类
- /// </summary>
- public class HttpHelper : MonoSingleton<HttpHelper>
- {
- public void Get(string url, Action<string> OnResult, Action<string> OnErr)
- {
- StartCoroutine(GetRequest(url, OnResult, OnErr));
- }
- public void Post(string url, List<KeyValuePair<string, string>> paras, Action<string> OnResult, Action<string> OnErr)
- {
- // CoroutineWrapper.Instance.EXEDelayFrames();
- StartCoroutine(PostRequest(url, paras, OnResult, OnErr));
- }
- #region ' 网络下载功能 '
- // todo: 这里考虑增设cache设计,扩展下载和缓存功能.
- // 对于这种资源没必要每次都完整下载,这样多浪费啊,都是钱啊.
- // -by gwang 2017年1月20日 16:41:17
- // 经验证: 在WWW中加入header if-modified-since字段可以得倒304(Nginx服务器,资源为txt类型)
- // 后续需要做的是一个文件管理系统,管理本地缓存文件的时间戳.
- // 当调用下载的时候,先检查缓存中的文件,拿到本地时间戳(FileInfo.LastWriteTimeUtc.ToString("r")),然后请求服务器,
- // 这样,虽然每次都请求了,但是不必每次都下发完整的文件,如果没有更新的情况下,只需返回304即可.
- #endregion
- //
- #region 私有方法
- IEnumerator GetRequest(string url, Action<string> OnResult, Action<string> OnErr)
- {
- using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
- {
- yield return webRequest.SendWebRequest();
- if (!string.IsNullOrEmpty(webRequest.error))
- {
- OnErr?.Invoke(webRequest.error);
- }
- else
- {
- OnResult?.Invoke(webRequest.downloadHandler.text);
- }
- }
- }
- ///
- IEnumerator PostRequest(string url, List<KeyValuePair<string, string>> paras, Action<string> OnResult, Action<string> OnErr)
- {
- WWWForm form = new WWWForm();
- paras.ForEach(kv => form.AddField(kv.Key, kv.Value)); // 压入参数
- using (UnityWebRequest webRequest = UnityWebRequest.Post(url, form))
- {
- yield return webRequest.SendWebRequest();
- if (!string.IsNullOrEmpty(webRequest.error))
- {
- OnErr?.Invoke(webRequest.error);
- }
- else
- {
- OnResult?.Invoke(webRequest.downloadHandler.text);
- }
- }
- }
- #endregion
- }
- }
|