没有网络时WWW冻结 [英] WWW freezing when there is no internet
问题描述
我正在Unity中编写一个简单的代码,以检查是否能够通过我的应用程序访问网站.这是我编写的代码:
I am writing a simple code in Unity, to check if I am able to reach a website through my app. This is the code I have written:
IEnumerator CheckInternetPing()
{
WWW wwwInternet = new WWW("http://google.com");
yield return wwwInternet;
if (wwwInternet.bytesDownloaded == 0)
{
//yield return new WaitForSeconds(1f);
Debug.Log("Not Connected to Internet");
}
else
{
Debug.Log("Connected to Internet");
internetMenu.SetActive(false);
}
}
我发现了一个错误,如果我在打开Internet的情况下运行它,则显示已连接",但是当我关闭Internet并在此后立即运行该应用程序时,它不记录任何内容.仅当我再次重新启动应用程序时,它才会显示未连接". 有谁知道为什么它在第一时间什么都不记录?谢谢
I have found a bug where if I run this with my Internet on, it shows "Connected", but when I switch the Internet off and run the app immediately after, it logs nothing. It shows "Not Connected" only if I restart the app another time. Does anyone know why it logs nothing during the first time? Thanks
推荐答案
这是带有WWW
类的 bug ,已经存在了很长时间.每个设备的行为可能不同.如果禁用了Wifi,它通常会冻结在编辑器上.快速测试表明此错误尚未修复.
This is a bug with the WWW
class and has been here for a long time. The behavior is probably different every device. It used to freeze on the Editor if Wifi is disabled. A quick test showed that this bug has not been fixed.
您需要使用HttpWebRequest
而不是WWW
.
在下面的示例中,Thread
用于避免请求阻塞Unity程序,而UnityThread
用于在请求完成后使回调进入Unity主线程.从
In the example below, Thread
is used to avoid the request blocking Unity program and UnityThread
is used to make callback into Unity main Thread when the request is done. Get UnityThread
from this post.
void Awake()
{
//Enable Callback on the main Thread
UnityThread.initUnityThread();
}
void isOnline(Action<bool> online)
{
bool success = true;
//Use ThreadPool to avoid freezing
ThreadPool.QueueUserWorkItem(delegate
{
try
{
int timeout = 2000;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://google.com");
request.Method = "GET";
request.Timeout = timeout;
request.KeepAlive = false;
request.ServicePoint.Expect100Continue = false;
request.ServicePoint.MaxIdleTime = timeout;
//Make sure Google don't reject you when called on mobile device (Android)
request.changeSysTemHeader("User-Agent", "Mozilla / 5.0(Windows NT 10.0; WOW64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 55.0.2883.87 Safari / 537.36");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response == null)
{
success = false;
}
if (response != null && response.StatusCode != HttpStatusCode.OK)
{
success = false;
}
}
catch (Exception)
{
success = false;
}
//Do the callback in the main Thread
UnityThread.executeInUpdate(() =>
{
if (online != null)
online(success);
});
});
}
您需要changeSysTemHeader
函数的扩展类,该扩展类允许更改"User-Agent" 标头:
You need the extension class for the changeSysTemHeader
function that allows the "User-Agent" header to be changed:
public static class ExtensionMethods
{
public static void changeSysTemHeader(this HttpWebRequest request, string key, string value)
{
WebHeaderCollection wHeader = new WebHeaderCollection();
wHeader[key] = value;
FieldInfo fildInfo = request.GetType().GetField("webHeaders",
System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Instance
| System.Reflection.BindingFlags.GetField);
fildInfo.SetValue(request, wHeader);
}
}
使用起来真的很简单:
void Start()
{
isOnline((online) =>
{
if (online)
{
Debug.Log("Connected to Internet");
//internetMenu.SetActive(false);
}
else
{
Debug.Log("Not Connected to Internet");
}
});
}
这篇关于没有网络时WWW冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!