当前位置:网站首页>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);
}
}
}
边栏推荐
- 2022giao考游记
- ORACLE - 数据查询
- 关闭MongoDB一些服务需要注意的地方(以及开启的相关命令)
- APP-新功能上线
- Flutter 網絡請求封裝之Dio(Cookie管理、添加攔截器、下載文件、异常處理、取消請求等)
- HotSpot JVM 「01」类加载、链接和初始化
- Jingwei Hengrun is registered through the science and Innovation Board: it plans to raise 5billion yuan, with a 9-month revenue of 2.1 billion yuan
- 数据治理,说起来容易,做起来难
- Relinearization in homomorphic encryption (ckks)
- Interview shock 23: talk about thread life cycle and transformation process?
猜你喜欢
What is 5g? What can 5g do? What will 5g bring in the future?
Cvpr2022 tutorial | machine learning remote sensing processing: agriculture and food security, University of Maryland
2022-2028 global extrusion coating and lamination production line industry research and trend analysis report
2022-2028 global TFT touch screen industry research and trend analysis report
Why absolute positioning overlaps
2022-2028 global industrial touch screen industry research and trend analysis report
腾讯《和平精英》新版本将至:新增账号安全保护系统,游戏内违规行为检测升级
Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
Oracle - 基本入门
数据治理,说起来容易,做起来难
随机推荐
民航局:到 2025 年我国将初步建成安全、智慧、高效和绿色的航空物流体系
Unity技术手册 - 粒子发射和生命周期内速度子模块
作为一个程序员我们如何快乐的学习成长进步呢?(个人感悟和技术无关)
How to disable the optical drive
27 Chinese scholars including Yaoban and chendanqi from Tsinghua won the awards, and the list of winners of Sloan award in 2022 was issued
How to open a futures account safely at present? Which futures companies are more reliable?
How to guarantee idempotency of message queue
Talk about adapter mode
[WPF] XAML code skills that can be directly used for converting CAD engineering drawings to WPF
How to design a complex business system? From the understanding of domain design, cloud native, micro service, and middle platform
2022 love analysis · panoramic report of it operation and maintenance manufacturers
Some reflections on preparing for the Blue Bridge Cup
Analysis report on market demand situation and investment direction of China's optical transmission equipment industry from 2022 to 2028
ES6-- 模板字符串、对象的简化写法、箭头函数
Use apiccloud AVM multi terminal component to quickly realize the search function in the app
2022-2028 global variable frequency compressor technology industry research and trend analysis report
ES6-Const常量与数组解构
Record the learning record of the exists keyword once
adb常用命令
ORACLE - 数据查询