private IEnumerator FetchDataSync <T>(string url, OnDataFetched <T> callback, float delay, float tryToResend) { if (delay != 0) { yield return(new WaitForSeconds(delay)); } CLog.Log(this, "Loading " + url); WWW www = new WWW(url); yield return(www); if (www.error == null) //load successfully { //save data to use next times PlayerPrefs.SetString(GetKey <T>(), www.text); T data = GetData <T>(); if (callback != null) { callback(data); } } else { if (tryToResend > 0) { StartCoroutine(FetchDataSync <T>(url, callback, tryToResend, tryToResend)); } } }
protected override void Run() { ProcessStartInfo psi = new ProcessStartInfo("casperjs", "netease.js \"" + TargetUser + "\" \"" + TargetUser + ".json\"") { RedirectStandardOutput = false, UseShellExecute = false }; //psi.RedirectStandardOutput = true; //psi.Verb = "RunAs"; if (IsFirstRun && !IsTest) { var randomOffset = new Random().Next(0, 1000 * 600); waitToken.WaitHandle.WaitOne(randomOffset); } Process fetchProc = Process.Start(psi); fetchProc.WaitForExit(); if (fetchProc.ExitCode != 0) { Console.WriteLine("Fetch Failed."); } else { string ret = File.ReadAllText(TargetUser + ".json"); JObject obj = JObject.Parse(ret); OnDataFetched?.Invoke(obj); //Console.WriteLine("Message Fetched."); } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url"></param> /// <param name="callback"></param> /// <param name="tryToResend"></param> public void FetchData <T>(string url, OnDataFetched <T> callback = null, float tryToResend = 10f) { StartCoroutine(FetchDataSync <T>(url, callback, 0, tryToResend)); }