public static void Ping(string host, float timeOut = 4f, int pingTimes = 4, Action <string, PingStatistics> resultCallBack = null) { StringBuilder builder = new StringBuilder(); builder.Append("Start Ping " + host + "\n"); UnityPing.CreatePing(host, (res) => { string stateInfo = res.isSuccess ? "Success" : res.errorReason.ToString(); builder.Append("return:" + stateInfo + " time:" + res.pingTime + "\n"); }, OnComplete: (pingResults) => { int passTimes = 0; int maxTime = 0; int minTime = -1; int allTime = 0; int caculateTimes = 0; string ip = ""; foreach (var item in pingResults) { ip = item.ip; if (item.isSuccess) { passTimes++; if (maxTime < item.pingTime) { maxTime = item.pingTime; } if (minTime == -1) { minTime = item.pingTime; } if (minTime > item.pingTime) { minTime = item.pingTime; } allTime += item.pingTime; caculateTimes++; } } int averagePingTime = caculateTimes == 0? 0:(allTime / caculateTimes); PingStatistics statistics = new PingStatistics(host, ip, pingResults.Length, passTimes, maxTime, minTime, averagePingTime); builder.Append("Ping Times:" + statistics.pingTimes + " Pass Times:" + statistics.pingPass + "\n"); if (passTimes > 0) { builder.Append("Min Ping:" + statistics.minPingTime + "ms Max Ping:" + statistics.maxPingTime + "ms AVG:" + statistics.averagePingTime + "\n"); } if (resultCallBack != null) { resultCallBack(builder.ToString(), statistics); } else { Debug.Log(builder); } }, timeOut: timeOut, runTimes: pingTimes); }
private void OnDestroy() { s_ip = string.Empty; s_timeout = 20; s_callback = null; if (s_unityPing != null) { s_unityPing = null; } }
public static void CreatePing(string ip, Action <int> callback) { if ((!string.IsNullOrEmpty(ip) && (callback != null)) && (s_unityPing == null)) { ip = "14.215.177.39"; s_ip = ip; s_callback = callback; GameObject target = new GameObject("UnityPing"); Object.DontDestroyOnLoad(target); s_unityPing = target.AddComponent <UnityPing>(); } }
public static UnityPing CreatePing(string host, Action <PingResult> pingResultCallback, Action <PingResult[]> OnComplete = null, float timeOut = 5f, int runTimes = 1) { if (string.IsNullOrEmpty(host)) { return(null); } if (pingResultCallback == null) { return(null); } GameObject go = new GameObject("[UnityPing]"); DontDestroyOnLoad(go); UnityPing s_unityPing = go.AddComponent <UnityPing>(); s_unityPing.s_pingResultCallback = pingResultCallback; s_unityPing.s_OnComplete = OnComplete; s_unityPing.StartRun(host, timeOut, runTimes); return(s_unityPing); }
public static void CreatePing(string ip, System.Action <int> callback) { if (string.IsNullOrEmpty(ip)) { return; } if (callback == null) { return; } if (s_unityPing != null) { return; } s_ip = ip; s_callback = callback; GameObject go = new GameObject("UnityPing"); DontDestroyOnLoad(go); s_unityPing = go.AddComponent <UnityPing>(); }