当前位置:网站首页>Unity的Ping类使用
Unity的Ping类使用
2022-06-25 21:50:00 【Smart_zy】
1.目的
1.1 准备ping网络,查看是否能够ping通,ping通代表网络链接正常
2.参考
2.1
Unity 之 Ping类简析&尝试使用_陈言必行的博客-CSDN博客
3.注意
3.1 发现不怎么好用,
- 明明能够ping通,却显示ping不通
- 里面没有变量显示 是否ping的通,
- ping_zhuJi.time=-1 ms ,实际上电脑可以ping通,

代码如下
- 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);
}
}
}
边栏推荐
- Unity技术手册 - 生命周期内颜色ColorOverLifetime--速度颜色ColorBySpeed
- Trillions of hot money smashed into the space economy. Is it really a good business?
- Tiger DAO VC产品正式上线,Seektiger生态的有力补充
- 哪些PHP开源作品值得关注
- 【WPF】CAD工程图纸转WPF可直接使用的xaml代码技巧
- App test points
- Privatization lightweight continuous integration deployment scheme -- 03 deployment of Web services (Part 2)
- 目前期货怎么开户安全些?哪些期货公司靠谱些?
- 字符串变形(字符串大小写切换和变现)
- 2022-2028 global TFT LCD touch screen industry research and trend analysis report
猜你喜欢

CVPR2022教程 | 马里兰大学《机器学习遥感处理:农业与粮食安全》教程

万亿热钱砸向太空经济,真的是一门好生意?

2022-2028 global industrial TFT LCD industry survey and trend analysis report

Glory launched the points mall to support the exchange of various glory products

2022-2028 global RBI platform industry research and trend analysis report

Why is BeanUtils not recommended?

ES7/ES9 -- 新特性与正则
![[intensive lecture] 2022 PHP intermediate and advanced interview questions (II)](/img/50/c9572beaa035d5e6e78a57fcd1302c.jpg)
[intensive lecture] 2022 PHP intermediate and advanced interview questions (II)

Nacos source code analysis 01 code structure

2022-2028 global selective laser sintering service industry research and trend analysis report
随机推荐
Lecture 14 of the Blue Bridge Cup -- number theory [example]
Hotspot JVM "01" class loading, linking and initialization
Analysis report on scale investigation and investment development suggestions of China's special equipment inspection and testing industry 2022-2028
MATLAB Programming Notes
2022-2028 global horizontal reciprocating compressor industry research and trend analysis report
Eureka core ⼼ source code analysis
What should it personnel over 35 years old do if they are laid off by the company one day?
CVPR2022教程 | 马里兰大学《机器学习遥感处理:农业与粮食安全》教程
C language and the creation and use of database
Jz-064- maximum value of sliding window
Wpewebkit debugging MSE playback
2022-2028 global transmission type photoelectric circuit breaker industry research and trend analysis report
ES6-Const常量与数组解构
2022-2028 global open source cloud storage industry research and trend analysis report
[invitation letter] on March 4, the platform enabled digital intelligence Innovation -- UFIDA BiP PAAS cloud platform IUAP digital intelligence hundred cities forum · Jinan Station
Ribbon core ⼼ source code analysis
Why is BeanUtils not recommended?
OSPF - detailed explanation of GRE tunnel (including configuration command)
Yyds dry goods inventory CEPH installation visual dashboard
HotSpot JVM 「01」类加载、链接和初始化