How to download large files with unity WebGL without timeout
I am trying to port my work to the new WebGL build target with unity 5.1.1f1.
The bug is solved in unity 5.1.1p1, so the solution below is for 5.1.1f1.
My client is downloading big files (textures) from the server and the WWW class error randomly showed ‘Unknown Error’.
In the resulting JS file I had to modify the following code to output the reason for failure along with the ‘Unkonw Error’ message, and the result was timeout exceeded.
http.onerror = function http_onerror(e) {
HandleError ("Unknown error. " + e.toString());
};
I did some research and I found the resulting code sets the timeout for the XMLHttpRequest object to a constant 5000 milliseconds in the resulting JS file.
Since it’s hard to replace this every time I build the project, I wrote a post process that does this job.
[PostProcessBuildAttribute(1)]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) {
UnityEngine.Debug.Log( "post build: "+pathToBuiltProject );
if (target == BuildTarget.WebGL)
{
string path = pathToBuiltProject;
string lastDirectoryName = Path.GetFileName(pathToBuiltProject);
UnityEngine.Debug.Log("post build2: " + lastDirectoryName);
string developmentDir = pathToBuiltProject+ "/Development/" + lastDirectoryName + ".js";
string releaseDir = pathToBuiltProject+ "/Release/" + lastDirectoryName + ".js";
UnityEngine.Debug.Log("post build3: " + developmentDir);
//File.WriteAllText("Path", Regex.Replace(File.ReadAllText("Path"), "[Pattern]", "Replacement"));
File.WriteAllText(developmentDir, File.ReadAllText(developmentDir).Replace("_JS_WebRequest_SetTimeout(($$lcssa8|0),5000);", "_JS_WebRequest_SetTimeout(($$lcssa8|0),60000);"));
File.WriteAllText(releaseDir, File.ReadAllText(releaseDir).Replace("_JS_WebRequest_SetTimeout(($$lcssa8|0),5000);", "_JS_WebRequest_SetTimeout(($$lcssa8|0),60000);"));
Process proc = new Process();
proc.StartInfo.FileName = "http://localhost:810";
proc.Start();
}
}
I will keep the blog updated as I get rid of webgl trouble!