I'm moving a multiplayer application of mine from XNA over to Unity so that I can make use of the web player. To make things work with the Web Player, it seems I have to use the WWW class instead of HttpWebRequest. My tests are showing that accessing the exact same page from a Unity application takes 10 times longer using WWW than it does using HttpWebRequest. Is that to be expected? Is there anything I can do to improve WWW's performance?
In my specific case, the WWW request takes about 450 ms (around half a second) to fully execute, while the HttpWebRequest is taking 47 ms - less than half of a tenth of a second!
I’ve had some success increasing WWW request performance by building the POST data myself rather than using the WWWForm object:
private string generatePostString(){
//MW: Generate a random salt for this request
System.Random random = new System.Random();
int randomNumber = random.Next(0, 9999999);
//MW: hash the salt and secret key
string salt = randomNumber.ToString();
string hashedKey = Md5Sum(mSecretKey + salt);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendLine("hashedKey="+ hashedKey +"&salt="+ salt +"&deviceType=5&bindingVersion=0.1");
foreach (DictionaryEntry item in mParameters){
//MW: TODO We need to distinguish between data types and correctly handle each one.
sb.AppendLine("¶ms["+item.Key.ToString()+"]="+item.Value.ToString());
}
return sb.ToString();
}
private byte[] convertPostStringToData(string postString){
//MW: convert the string into a byte array
ASCIIEncoding encoding = new ASCIIEncoding();
return encoding.GetBytes(postString);
}
Also, I suspect the additional time for a WWW request is because it has to load the crossdomain.xml for every request which is pretty annoying.