当前位置:网站首页>The Ping class of unity uses
The Ping class of unity uses
2022-06-25 22:59:00 【Smart_ zy】
1. Purpose
1.1 Get ready ping The Internet , See if you can ping through ,ping Pass means that the network link is normal
2. Reference resources
2.1
Unity And Ping Class analysis & Try to use _ Chen yanbixing's blog -CSDN Blog
3. Be careful
3.1 It is not easy to use ,
- Obviously can ping through , But it shows that ping no
- There are no variables in it whether ping Tong ,
- ping_zhuJi.time=-1 ms , In fact, computers can ping through ,

The code is as follows
- ping_zhuJi.time Mainly with this and 0 Compare and judge whether it is linked
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 【Function:ping The Internet 】【Time:2022 06 20】【Author:XZY】
/// </summary>
public class PingNet : MonoBehaviour
{
/// <summary> The host ip, The purpose is to ping Is it open </summary>
[HideInInspector]
//public string ip_zhuJi = "192.168.1.111";
public string ip_zhuJi = "";
/// <summary>Ping object : The host </summary>
Ping ping_zhuJi=null;
/// <summary>Ping object : Backstage </summary>
Ping ping_server;
/// <summary> coroutines : Check whether it hasn't been for a long time ping through , If representative ping no </summary>
private Coroutine coroutine_isPingSuccess=null;
/// <summary>ping One time </summary>
private float waitForSeconds_IE_isPingSuccess = 5;
/// <summary>ping The number of failures , With the process waitForSeconds_IE_isPingSuccess It matters </summary>
private int numPingFail = 0;
/// <summary>ping How many times are failures </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 Address : Get the entire address of the host : " + _ip);
// string[] str_add = _ip.Split(':');
// for (int i = 0; i < str_add.Length; i++)
// {
// Debug.Log("PingNet Address : Get the host's ip: The first " + i + " A field :" + str_add[i]);
// }
// string str_add2 = str_add[1].Trim('/');
// Debug.Log("PingNet Address : Get the host's ip: " + str_add2);
//}
//IEnumerator IE_StartPing()
//{
// yield return new WaitForSeconds(6);
// string _ip = ip_zhuJi;
// if (_ip == "")
// {
// Debug.Log("PingNet: Address : The host ip It's empty , Please check ");
// 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: Get the host's ip】【Time:2022 06 21】【Author:XZY】
/// </summary>
string GetIp_zhuJi()
{
string _ip = SmartPlayerAndroidMono.Instance.ip_zhuJi_all;
Debug.Log("PingNet Address : Get the entire address of the host : " + _ip);
string[] str_add = _ip.Split(':');
for (int i = 0; i < str_add.Length; i++)
{
Debug.Log("PingNet Address : Get the host's ip: The first " + i + " A field :" + str_add[i]);
}
string str_add2 = str_add[1].Trim('/');
Debug.Log("PingNet Address : Get the host's ip: " + str_add2);
return str_add2;
}
/// <summary>
/// 【Function: Start ping】【Time:2022 06 20】【Author:XZY】
/// </summary>
public void StartPing()
{
string _ip = GetIp_zhuJi();
if (_ip == "")
{
Debug.Log("PingNet: Address : The host ip It's empty , Please check ");
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: send out ping】【Time:2022 06 20】【Author:XZY】
/ </summary>
//void SendPing()
//{
// ping_zhuJi = new Ping(ip_zhuJi);
//}
/// <summary>
/// 【Function: coroutines : whether ping success 】【Time:2022 06 20】【Author:XZY】
/// </summary>
/// <returns></returns>
IEnumerator IE_isPingSuccess()
{
while (true)
{
//Debug.Log(" Address :IE_isPingSuccess:");
yield return new WaitForSeconds(waitForSeconds_IE_isPingSuccess);
Debug.Log("PingNet Address :null != ping_zhuJi:" + (null != ping_zhuJi));
Debug.Log("PingNet Address :ping_zhuJi.isDone:" + ping_zhuJi.isDone);
// Only through delayTime Of <0 Determine whether ping through , if <0 That is, no ping through ; conversely ping through
if (null != ping_zhuJi && ping_zhuJi.isDone)
{
float delayTime = ping_zhuJi.time;
if (delayTime <= 0)
{
numPingFail++;
Debug.Log("PingNet Address :ip_zhuJi:" + ip_zhuJi + ":ping The number of failures :" + numPingFail);
Debug.Log("PingNet Address :Ping: I haven't had it for a long time ping through ");
// break link UI Show
ConnectTipPanel.instance.AddConnectFail();
}
else
{
Debug.Log("PingNet Address :ping_zhuJi:" + ip_zhuJi + ":" + delayTime.ToString() + " ms");
numPingFail = 0;
Debug.Log("PingNet Address :ip_zhuJi:" + ip_zhuJi + ":ping success ");
// break link UI hide
ConnectTipPanel.instance.ConnectSuccess();
}
}
ping_zhuJi.DestroyPing();
ping_zhuJi = null;
ping_zhuJi = new Ping(ip_zhuJi);
}
}
}
边栏推荐
- Reasons why MySQL cannot be connected externally after installing MySQL database on ECs and Solutions
- Development trend of China's power carrier communication industry and Research Report on the 14th five year plan 2022 ~ 2028
- Tiger Dao VC products are officially launched, a powerful supplement to seektiger ecology
- ES6 --- 数值扩展、对象拓展
- This 110 year old "longevity" enterprise has been planning for the next century
- How to guarantee idempotency of message queue
- ES6-- 集合
- Dio encapsulated by the flutter network request (cookie management, adding interceptors, downloading files, exception handling, canceling requests, etc.)
- What are the channels for Internet advertising to gain customers?
- 民航局:到 2025 年我国将初步建成安全、智慧、高效和绿色的航空物流体系
猜你喜欢

Obsidian基础教程

2022-2028 global industrial TFT LCD industry survey and trend analysis report
![Lecture 14 of the Blue Bridge Cup -- number theory [exercises]](/img/96/0971909c8bf25820c2d4f520bb83fb.jpg)
Lecture 14 of the Blue Bridge Cup -- number theory [exercises]

OSPF - detailed explanation of GRE tunnel (including configuration command)

Why is BeanUtils not recommended?

简单好用的缓存库 gcache

2022-2028 global TFT LCD touch screen industry research and trend analysis report

Canoe: the fifth simulation project: simulation + test

1281_FreeRTOS_vTaskDelayUntil实现分析

Talk about adapter mode
随机推荐
Nacos source code analysis 01 code structure
Why can't the mobile phone be used and the computer be connected
Oracle - 基本入门
The difference between synchronize and volatile
Wpewebkit debugging MSE playback
【WPF】CAD工程图纸转WPF可直接使用的xaml代码技巧
NRM source switching tool
字符串变形(字符串大小写切换和变现)
Obsidian basic tutorial
Ribbon core ⼼ source code analysis
How to guarantee idempotency of message queue
不荒唐的茶小程序-规则改动
2022-2028 global co extrusion production line industry research and trend analysis report
Flutter 網絡請求封裝之Dio(Cookie管理、添加攔截器、下載文件、异常處理、取消請求等)
Unity技术手册 - 粒子发射和生命周期内速度子模块
Global and Chinese flame retardant ABS industry development trend and market demand analysis report 2022 ~ 2028
Simple and easy-to-use cache library gcache
Use apiccloud AVM multi terminal component to quickly realize the search function in the app
Unity technical manual - particle emission and life cycle velocity sub module
Lecture 14 of the Blue Bridge Cup -- number theory [example]