Unity的Ping类使用

1.目的

1.1 准备ping网络,查看是否能够ping通,ping通代表网络链接正常

2.参考

2.1

Unity 之 Ping类简析&尝试使用_陈言必行的博客-CSDN博客

3.注意

3.1 发现不怎么好用,

  1. 明明能够ping通,却显示ping不通
  2. 里面没有变量显示 是否ping的通,
  3. ping_zhuJi.time=-1 ms ,实际上电脑可以ping通,

代码如下

  1. ping_zhuJi.time主要以这个和0比较判断是否链接上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 【Function:ping网络】【Time:2022 06 20】【Author:XZY】
/// </summary>
public class PingNet : MonoBehaviour
{
    /// <summary>主机的ip,目的为了ping它是否通</summary>
    [HideInInspector]
    //public string ip_zhuJi = "192.168.1.111";
    public string ip_zhuJi = "";

    /// <summary>Ping对象:主机的</summary>
    Ping ping_zhuJi=null;
    
    /// <summary>Ping对象:后台的</summary>
    Ping ping_server;

    /// <summary>协程:检测是否长时间没有ping通,如果是代表ping不通</summary>
    private Coroutine coroutine_isPingSuccess=null;

    /// <summary>ping一次的时间</summary>
    private float waitForSeconds_IE_isPingSuccess = 5;

    /// <summary>ping失败的次数,与协程waitForSeconds_IE_isPingSuccess有关系</summary>
    private int numPingFail = 0;

    /// <summary>ping失败多少次为失败</summary>
    private int totalPingFail = 3;

    public static PingNet instance = null;

    //public int NumPingFail { get => numPingFail; set => numPingFail = value; }

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
    }

    private void Start()
    {
        //coroutine_isPingSuccess = StartCoroutine(IE_isPingSuccess());

        //StartCoroutine(IE_GetIp_zhuJi());

        //StartCoroutine(IE_StartPing());
    }

    //IEnumerator IE_GetIp_zhuJi()
    //{
    //    yield return new  WaitForSeconds(5);

    //    string _ip = SmartPlayerAndroidMono.Instance.ip_zhuJi;
    //    Debug.Log("PingNet地址:得到主机的整个地址:   " + _ip);

    //    string[] str_add = _ip.Split(':');
    //    for (int i = 0; i < str_add.Length; i++)
    //    {
    //        Debug.Log("PingNet地址:得到主机的ip:第 " + i + "个字段:" + str_add[i]);
    //    }

    //    string str_add2 = str_add[1].Trim('/');
    //    Debug.Log("PingNet地址:得到主机的ip:   " + str_add2);
    //}

    //IEnumerator IE_StartPing()
    //{
    //    yield return new WaitForSeconds(6);

    //    string _ip = ip_zhuJi;

    //    if (_ip == "")
    //    {
    //        Debug.Log("PingNet:地址:主机的ip为空,请检查");
    //        yield return null;
    //    }
    //    else
    //    {
    //        ip_zhuJi = _ip;
    //    }

    //    if (ping_zhuJi != null)
    //    {
    //        ping_zhuJi.DestroyPing();
    //        ping_zhuJi = null;
    //    }
    //    ping_zhuJi = new Ping(ip_zhuJi);

    //    if (coroutine_isPingSuccess != null)
    //    {
    //        StopCoroutine(IE_isPingSuccess());
    //        coroutine_isPingSuccess = null;
    //    }
    //    coroutine_isPingSuccess = StartCoroutine(IE_isPingSuccess());
    //}

   

    /// <summary>
    /// 【Function:得到主机的ip】【Time:2022 06 21】【Author:XZY】
    /// </summary>
    string GetIp_zhuJi()
    {
       string  _ip = SmartPlayerAndroidMono.Instance.ip_zhuJi_all;
        Debug.Log("PingNet地址:得到主机的整个地址:   " + _ip);

        string[] str_add = _ip.Split(':');
        for (int i = 0; i < str_add.Length; i++)
        {
            Debug.Log("PingNet地址:得到主机的ip:第 " + i + "个字段:" + str_add[i]);
        }

        string str_add2 = str_add[1].Trim('/');
        Debug.Log("PingNet地址:得到主机的ip:   " + str_add2);

        return str_add2;
    }

    /// <summary>
    /// 【Function:开始ping】【Time:2022 06 20】【Author:XZY】
    /// </summary>
    public void StartPing()
    {   
        string _ip = GetIp_zhuJi();

        if (_ip == "")
        {
            Debug.Log("PingNet:地址:主机的ip为空,请检查");
            return;
        }
        else
        {
            ip_zhuJi = _ip;
        }

        if (ping_zhuJi != null)
        {
            ping_zhuJi.DestroyPing();
            ping_zhuJi = null;
        }
        ping_zhuJi = new Ping(ip_zhuJi);

        if (coroutine_isPingSuccess != null)
        {
            StopCoroutine(IE_isPingSuccess());
            coroutine_isPingSuccess = null;
        }
        coroutine_isPingSuccess = StartCoroutine(IE_isPingSuccess());
    }

    / <summary>
    / 【Function:发送ping】【Time:2022 06 20】【Author:XZY】
    / </summary>
    //void SendPing()
    //{
    //    ping_zhuJi = new Ping(ip_zhuJi);
    //}

    /// <summary>
    /// 【Function:协程:是否ping成功】【Time:2022 06 20】【Author:XZY】
    /// </summary>
    /// <returns></returns>
    IEnumerator IE_isPingSuccess()
    {
        while (true)
        {
            //Debug.Log("地址:IE_isPingSuccess:");
            yield return new WaitForSeconds(waitForSeconds_IE_isPingSuccess);

            Debug.Log("PingNet地址:null != ping_zhuJi:" + (null != ping_zhuJi));
            Debug.Log("PingNet地址:ping_zhuJi.isDone:" + ping_zhuJi.isDone);

            //只能通过delayTime的<0判断是否ping通,若<0即没有ping通;反之ping通
            if (null != ping_zhuJi && ping_zhuJi.isDone)
            {
                float delayTime = ping_zhuJi.time;
                if (delayTime <= 0)
                {
                    numPingFail++;
                    Debug.Log("PingNet地址:ip_zhuJi:" + ip_zhuJi + ":ping失败的次数:" + numPingFail);

                    Debug.Log("PingNet地址:Ping:长时间没有ping通");
                    //断开链接UI 显示
                    ConnectTipPanel.instance.AddConnectFail();
                }
                else
                {
                    Debug.Log("PingNet地址:ping_zhuJi:" + ip_zhuJi + ":" + delayTime.ToString() + " ms");
                    numPingFail = 0;
                    Debug.Log("PingNet地址:ip_zhuJi:" + ip_zhuJi + ":ping成功");

                    //断开链接UI 隐藏
                    ConnectTipPanel.instance.ConnectSuccess();
                }
            }
            ping_zhuJi.DestroyPing();
            ping_zhuJi = null;
            ping_zhuJi = new Ping(ip_zhuJi);
        }
    }

}