Search Unity

How to stop the www request ?

Discussion in 'Scripting' started by apple_motion, Dec 3, 2009.

  1. apple_motion

    apple_motion

    Joined:
    Jul 2, 2009
    Posts:
    169
    I do know how to use the www class to download an image from a website :)

    However, is it possible to cancel or stop it before the image is downloaded completely? (since, I want to reject the download)

    If that is not possible to cancel it within Unity, could it be do in the browser ?? ( I mean, using unity to issue or control an external javascript )

    Many Thanks
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    There's nothing in the Unity API to cancel a download. I doubt it is possible to do this from JavaScript in the web page either, but hopefully someone can prove me wrong.
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    the only way I could think of is refreshing the whole page including the player and thats not really an option.

    Though the WWW object is a regular .NET object (implements IDisposable), as such it also must implement Dispose()
    If that is implemented correctly, you should be able to kill it off right away by calling this function.

    If not, try if it helps to null the reference and call System.GC.Collect();
     
    Novack likes this.
  4. apple_motion

    apple_motion

    Joined:
    Jul 2, 2009
    Posts:
    169
    Did you mean the "WWW.Dispose()" ?? But, how to use it and when to use it. There have no description or sample code on the API document :p

    Thanks again :)

    PS. I already know how to clean up the memory destroy after the image is loaded.
    http://forum.unity3d.com/viewtopic.php?t=30739&highlight=dispose

    But, this time I want to "kill" the download process when the image still downloading :p
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    not WWW.Dispose() but yourWWWObjectInstance.Dispose()

    you won't find any documention on it in unity docs as it is no unity thing at all.
    But the WWW class implements the interface IDisposable and Dispose is a function that it therefor must implement.

    More on IDisposable can be found at go-mono and the msdn documentations
     
  6. Pedro

    Pedro

    Joined:
    Jan 21, 2008
    Posts:
    19
    I just tested Dispose method and it does indeed work. I called wwwObj.Dispose() and download stopped immediately. Woot.
    Works for both synchronous WWW object polling and asynchronous coroutine download methods.
     
    Mycroft likes this.
  7. abacus

    abacus

    Joined:
    Oct 22, 2010
    Posts:
    2
    wwwObj.Dispose() does not stop the www-request on iOS. But I found a solution. Here is how I did it (somewhat simplified from my original code, so there might be some typos or small mistakes below).

    I have a class MyLoader with two coroutines:
    Code (csharp):
    1.  
    2. Class MyLoader
    3. {
    4.     WWW m_www = null;
    5.  
    6.     public IEnumerator StartTimeout(float timeout)
    7.     {
    8.         yield return new WaitForSeconds(timeout);
    9.         ClearWWW();
    10.     }
    11.  
    12.     public IEnumerator Load(url)
    13.     {
    14.         m_www = new WWW(url);
    15. #if UNITY_IPHONE
    16.         while (m_www != null  !m_www.isDone) { yield return null; }
    17. #else
    18.         yield return m_www; // Cannot be interrupted by m_www.Dispose() on iOS
    19. #endif
    20.         ProcessResponse(m_www);
    21.         ClearWWW();
    22.     }
    23.  
    24.     public void ClearWWW()
    25.     {
    26.         if (m_www != null) {
    27.             m_www.Dispose();
    28.             m_www = null;
    29.             System.GC.Collect(); // Optional --- just runs the garbage collector
    30.         }
    31.     }
    32.  
    33.    ... // more stuff
    34. }
    35.  
    This class can be used as follows (from inside a method in a class that derives from MonoBehaviour):
    Code (csharp):
    1.  
    2. MyLoader myLoader = new MyLoader();
    3. StartCoroutine(myLoader.StartTimeout(5f)); // set a 5 second timeout
    4. yield return StartCoroutine(myLoader.Load("http://www.example.com"));
    5. Debug.Log("Either the url is finished loading, it used more than 5 seconds, or someone called myLoader.ClearWWW() by e.g. clicking on a cancel-button");
    6.  
    As you can see, if you change "yield return www" with the while-loop, it works on iOS too.
     
  8. kjuanlu

    kjuanlu

    Joined:
    Dec 4, 2011
    Posts:
    100
    After using WWW.Dispose(), if you check if the version is cached (with Caching.IsVersionCached, or LoadFromCacheOrDownload ), you will get a true (the version is cached, but the file is corrupted), so if you try to use LoadFromCacheOrDownload, your scene loading will fail.

    You have to use a version system, and use it when you use
    Code (csharp):
    1. WWW.LoadFromCacheOrDownload("http://url.com/assetBundle01.unity3d",mSingleton.cacheVersion01);
    and
    Code (csharp):
    1. Caching.IsVersionCached("http://url.com/assetBundle01.unity3d",mSingleton.cacheVersion01);
    And you should increase the value of mSingleton.cacheVersion01 after you call Dispose, and sore the cacheVersion01 value in the PlayerPref

    Something like this:

    Code (csharp):
    1. www.Dispose();
    2. mSingleton.cacheVersion01++;
    3. mSingleton.writeToDisk();
     
  9. OJ3D

    OJ3D

    Joined:
    Feb 13, 2014
    Posts:
    33
    @abacus - You're the bomb!
    I slightly modified your code. For some reason the Processresponse wasn't working (tried different namespaces) and added the WWW fetching..

    Thanks again


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Net;
    5.  
    6.  
    7. public class WWWLoadOrDispose
    8. {
    9.     WWW m_www = null;
    10.  
    11.     public IEnumerator StartTimeOut(float timeout)
    12.     {
    13.         yield return new WaitForSeconds(timeout);
    14.         Debug.Log("We've Timed out after " + timeout);
    15.         ClearWWW();
    16.  
    17.     }
    18.  
    19.     public IEnumerator Load (string url)
    20.  
    21.     {
    22.         //Go Fetch
    23.         m_www = new WWW(url);
    24.  
    25.         #if UNITY_IOS
    26.         while( m_www != null !m_www.isDone) {yield return null;}
    27.         #else
    28.         yield return m_www;//Cannot be interuppted by m_www.Dispose() on IOS
    29.         #endif      
    30.         Debug.Log("Here's what we got back" + m_www.text);
    31.         //ProcessResponse(m_www);
    32.         ClearWWW();
    33.     }
    34.  
    35.     public void ClearWWW()
    36.  
    37.     {
    38.         if(m_www != null)
    39.         {
    40.             m_www.Dispose();
    41.             m_www=null;
    42.             System.GC.Collect();// Optional --- just runs the garbage collector
    43.             Debug.Log("m_www URL has been disposed");
    44.         }
    45.  
    46.     }
    47.  
    48. }//END OF MAIN
    //Example of Running It

    Code (CSharp):
    1.     public IEnumerator Test(){
    2.  
    3.         WWWLoadOrDispose MyURL = new WWWLoadOrDispose();
    4.         StartCoroutine(MyURL.StartTimeOut(10f));//Set and Start a 10 second timeout
    5.         yield return StartCoroutine(MyURL.Load("https://www.google.com")); //Go Fetch  - Timeout after 10 sec regardless
    6.  
    7.     }
     
    Last edited: May 4, 2016
  10. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    In every scenario except large files that require multipart HTTP, I'm pretty sure the server will continue sending the file whether you've killed off your high-level receiving object or not. I only mention this in case someone wants to do this to avoid wasting bandwidth (often a consideration on mobile platforms, for example).
     
    jehovah0121qq likes this.