Unity3d 如何在没有fps掉线的情况下检查unity中的internet连接

Unity3d 如何在没有fps掉线的情况下检查unity中的internet连接,unity3d,websocket,connection,frame-rate,Unity3d,Websocket,Connection,Frame Rate,我在Unity做一个虚拟现实项目。在这个项目中,我们有WebSocket。对于某些internet问题,我们希望检查websocket是否仍然存在或出错,如果是这种情况,我们希望检查internet连接。如果internet再次启动,则需要重新连接到websocket或重新初始化它。现在,我们在更新功能中使用一个计时器,该计时器每秒启动一次,但每秒都有一个fps下降,用于检查互联网。还有别的办法吗 public float waitTime = 1f; private float timer;

我在Unity做一个虚拟现实项目。在这个项目中,我们有WebSocket。对于某些internet问题,我们希望检查websocket是否仍然存在或出错,如果是这种情况,我们希望检查internet连接。如果internet再次启动,则需要重新连接到websocket或重新初始化它。现在,我们在更新功能中使用一个计时器,该计时器每秒启动一次,但每秒都有一个fps下降,用于检查互联网。还有别的办法吗

public float waitTime = 1f;
private float timer;

private void Update()
{
    timer += Time.deltaTime;
    if (timer > waitTime)
    {
        //check if websocket is alive or that it must re initialized
        if (webSocket != null) //check if websocket was allready initialized 
        {
            if (isSocketError && Application.internetReachability != NetworkReachability.NotReachable) // check if an error was given by the websocket and ethernet is available again
            {
                CreateWebsocketSession(sessionId);
            }
            else if ((!webSocket.IsAlive || !webSocket.IsConnected) && Application.internetReachability != NetworkReachability.NotReachable) // check if websocket is alive and internet is available.
            {
                CreateWebsocketSession(sessionId);
            }
        }


        timer = 0f;
    }
}
当你调用一个函数时,它在返回之前运行到完成。这 实际上意味着函数中发生的任何操作都必须 在单帧更新中发生

我通常通过Google检查internet连接ping(
应用程序。internetReachability
不是确定实际连接的好方法:)

IEnumerator检查Internet连接(操作){
WWW=新WWW(“http://google.com");
yield return www;//等待该行的执行,在时间x执行
如果(www.error!=null){//在时间x+y执行,其中y是执行时间(我想在您有下降的地方)
行动(假);
}否则{
动作(正确);//这里有互联网
}
} 

在您的情况下,使用带有收益的
Coroutine()
而不是
Update()

而不是在Update函数中检查它,是否不可能在您想要执行的任何internet操作开始时检查它?您必须从更新中确定这一点。如果您正在使用该连接,您可以在传输数据的代码中检查该连接。问题是数据在随机点进入。在vr应用程序中,它可能会随机收到一条消息,我们希望检查websocket的internet状态是否处于活动状态,如果不是,则必须尽快重新连接。这就是为什么它会出现在更新中,因为用户没有交互。对于架构,我们有一个后端、一个虚拟现实设备和一个前端。其中后端是两个应用程序的套接字服务器。我们希望从前端>后端>虚拟现实设备发送数据。因此,没有用户输入,我们可以检查它
IEnumerator checkInternetConnection(Action<bool> action){
     WWW www = new WWW("http://google.com");
     yield return www;//wait for execution of this row, executed at Time x
     if (www.error != null) {//executed at time x+y, where y is the execution time(i think where you have drops)
         action (false);
     } else {
         action (true);//got internet here
     }
 }