unity ping 一個服務器 ip 的工具類

最近公司需要對服務器進行速度偵測,而不依賴於服務器返回的時間戳,所以需要使用 unity 自帶的 ping 類。

這裏提供一個現成的工具類:

using UnityEngine;
using System.Collections;

public class UnityPing : MonoBehaviour
{
    private static string s_ip = "";
    private static System.Action<int> s_callback = null;

    private static UnityPing s_unityPing = null;

    private static int s_timeout = 2;

    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>();
    }

    /// <summary>
    /// 超時時間(單位秒)
    /// </summary>
    public static int Timeout
    {
        set
        {
            if (value > 0)
            {
                s_timeout = value;
            }
        }
        get { return s_timeout; }
    }


    private void Start()
    {
        switch (Application.internetReachability)
        {
            case NetworkReachability.ReachableViaCarrierDataNetwork: // 3G/4G
            case NetworkReachability.ReachableViaLocalAreaNetwork: // WIFI
                {
                    StopCoroutine(this.PingConnect());
                    StartCoroutine(this.PingConnect());
                }
                break;
            case NetworkReachability.NotReachable: // 網絡不可用
            default:
                {
                    if (s_callback != null)
                    {
                        s_callback(-1);
                        Destroy(this.gameObject);
                    }
                }
                break;
        }
    }

    private void OnDestroy()
    {
        s_ip = "";
        s_timeout = 20;
        s_callback = null;

        if (s_unityPing != null)
        {
            s_unityPing = null;
        }
    }

    IEnumerator PingConnect()
    {
        // Ping網站 
        Ping ping = new Ping(s_ip);

        int addTime = 0;
        int requestCount = s_timeout * 10; // 0.1秒 請求 1 次,所以請求次數是 n秒 x 10

        // 等待請求返回
        while (!ping.isDone)
        {
            yield return new WaitForSeconds(0.1f);

            // 鏈接失敗
            if (addTime > requestCount)
            {
                addTime = 0;

                if (s_callback != null)
                {
                    s_callback(ping.time);
                    Destroy(this.gameObject);
                }
                yield break;
            }
            addTime++;
        }

        // 鏈接成功
        if (ping.isDone)
        {
            if (s_callback != null)
            {
                s_callback(ping.time);
                Destroy(this.gameObject);
            }
            yield return null;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章