Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jan 01, 2015 at 10:59 PM by Eluate for the following reason:

The question is answered, right answer was accepted

avatar image
3
Question by Eluate · Apr 18, 2014 at 09:22 AM · wwwphptimeout

How do you add a timeout to the WWW class?

Question: How do you add a timeout to the WWW class?

Explanation: Currently I'm creating an account system where it connects to a server/file in php format. If the server is down, how would I go about creating an error?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
4
Best Answer

Answer by fafase · Apr 18, 2014 at 10:15 AM

If the server is down, the request won't be successful so you will get an error.

 WWW www = new WWW(url);                                                 
 yield return www;
 if(www.error != null)print (www.error);

You can also create your own time out:

 WWW www = new WWW(url);  
 float timer = 0; 
 bool failed = false;
                    
 while(!www.isDone){
      if(timer > timeOut){ failed = true; break; }
      timer += Time.deltaTime;
      yield return null;
 }
 if(failed)www.Dispose();

The above needs to be tested for confirmation that it works.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Stream · Jan 30, 2016 at 08:18 PM 0
Share

It does not work on iOS

avatar image latas · Nov 24, 2016 at 09:08 AM 0
Share

On line #10 you have to use: if (failed || www.error != null )

If not, if the server is down or internet is not available, www.isDone is set to true, then you exit from the "while" loop, thinking everything went ok, and that's not true.

Cheers

avatar image im012 latas · Dec 13, 2018 at 09:06 AM 0
Share

how u solve this problem ? can u help

avatar image pep_dj · Jun 01, 2018 at 09:32 AM 0
Share

What about using this class?

 public class TimeoutWWW : WWW {
     private float timeout;
     private float elapsedTime;
 
     public TimeoutWWW(string url, float timeout) : base(url){
         this.timeout = timeout;
         this.elapsedTime = 0;
         this.timedOut = false;
     }
 
     public override bool keepWaiting{
         get{
             if (elapsedTime > timeout){
                 timedOut = true;
                 return false;
             }
             elapsedTime += Time.deltaTime;
             return base.keepWaiting;
         }
     }
 
     public bool timedOut{ get; private set; }
 }

Recommended way to use it is (so it's automatically disposed):

 using (TimeoutWWW www = new TimeoutWWW(url)) {
     yield return www;
 
     if (www.timedOut) {
         // Do something
     }
     else if (string.IsNullOrEmpty(www.error)) {
         // Do something
     }
     else {
         // Response received succefully
     }
 }


avatar image
5

Answer by DavidHidvegi · Sep 07, 2017 at 10:09 AM

The Best Answer does not work on iOS and its not correct.

This will work:

 IEnumerator DownloadFileWithTimeout(string URL)
 {
                     WWW www = new WWW(URL);
                     float timer = 0;
                     float timeOut = 10;
                     bool failed = false;
         
                     while (!www.isDone)
                     {
                         if (timer > timeOut) { failed = true; break; }
                         timer += Time.deltaTime;
                         yield return null;
                     }
                     if (failed || !string.IsNullOrEmpty(www.error))
                     {
                         www.Dispose();
                         yield break;
                     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pep_dj · May 31, 2018 at 07:19 AM 0
Share

Is "yield break" needed here? What does it do?

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

connecting to php? 2 Answers

Network class vs php for authoritative actions? 1 Answer

UnityWebRequest timing out ? Send() finishes but isDone = false 1 Answer

Retrieving varibale values from a php script? 1 Answer

Why is my PHP echo returning entire PHP file after WWW request? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges