- Home /
yield return on WWW never coming back?
I'm making a web hit with the WWW class. Something I have done a dozen times in other places but for some reason, although the hit to the server hits and comes back, the Unity client never picks the response up. I'm wondering if it's something quirky about WWW that I just haven't experienced before. I did some searching but everyone else having this problem is just because they didn't use StartCoroutine when initiating the communication. This isn't my problem.
Code calling method that yield returns the WWW object.
StartCoroutine(_director.ProcessInstant(new Battleschool.Common.Entities.Contract.InstantDetail { AbilityId = _ability.AbilityId, GameId = BattleStatics.Instance.GameId, RoundIndex = (short)BattleStatics.Instance.RoundIndex, SourceId = _sourceCharacterData.CharacterId, TargetId = _targetCharacters.First().CharacterId }));
The method making the hit itself.
internal IEnumerator ProcessInstant(InstantDetail details)
{
WWW www = new WWW(DirectorHelper.GetUri("Battle/ProcessInstant"), DirectorHelper.PrepareBody<InstantDetail>(details), DirectorHelper.GetHeaders());
yield return www;
// execution never makes it to this point
if (OnProcessInstantComplete != null)
{
OnProcessInstantComplete(this, DirectorHelper.DeserializeAsBase64FromNetwork<IInstantResponse>(www.text));
}
}
Here's the response coming back from the server as captured by Fiddler.
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Sat, 07 Feb 2015 16:34:20 GMT
X-AspNet-Version: 4.0.30319
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Content-Type: application/json; charset=utf-8
Content-Length: 686
Connection: Close
"AAEAAAD/////AQAAAAAAAAAMAgAAAExCYXR0bGVzY2hvb2wuQ29udHJhY3QsIFZlcnNpb249MS4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1udWxsBQEAAAA0QmF0dGxlc2Nob29sLkNvbW1vbi5FbnRpdGllcy5Db250cmFjdC5EaXZpbmVSZXNwb25zZQcAAAAcPFRvdGFsSGVhbHRoPmtfX0JhY2tpbmdGaWVsZB48Q3VycmVudEhlYWx0aD5rX19CYWNraW5nRmllbGQaPFRvdGFsTWFuYT5rX19CYWNraW5nRmllbGQcPEN1cnJlbnRNYW5hPmtfX0JhY2tpbmdGaWVsZBY8TGV2ZWw+a19fQmFja2luZ0ZpZWxkFjxDbGFzcz5rX19CYWNraW5nRmllbGQUPEtleT5rX19CYWNraW5nRmllbGQAAAAAAAEBCAgICAgCAAAAWgAAAFoAAAAyAAAAMgAAAAEAAAAGAwAAAAlEaXNtaXNzZXIGBAAAAAZEaXZpbmULAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
Appreciate any insights into this.
-=-=-UPDATE-=-=-
Incidentally, I moved to a completely synchronous approach by removing the StartCoroutine and changing the method making the hit to the below and it works fine. Of course, this can't serve as a solution, but thought it was interesting.
internal void ProcessInstant(InstantDetail details)
{
WWW www = new WWW(DirectorHelper.GetUri("Battle/ProcessInstant"), DirectorHelper.PrepareBody<InstantDetail>(details), DirectorHelper.GetHeaders());
while (!www.isDone) { }
//yield return www;
if (OnProcessInstantComplete != null)
{
OnProcessInstantComplete(this, DirectorHelper.DeserializeAsBase64FromNetwork<IInstantResponse>(www.text));
}
}
If I had this problem, I'd start checking that the content-length is correct. The body is utf8, so it's possible (but unlikely) that the number of bytes transferred differs from the content-length, so the WWW class is waiting for the correct number of bytes. Doesn't immediately explain why the sync version works. Sometimes the while loop consumes so much cpu that the WWW times out. You don't check to see if you have an error...
Thanks @Graham Dunnett. All of the data is serialized to a byte[] then Base64 encoded so it's not possible that the content length will be anything different than the length of the raw string in the body of the response. I have checked that the length of the string also matches the Content-Length header. You're right, the code isn't looking for an error and handling it, I will add that as I finish this up. That also can't be related to the problem unfortunately since checking for an error still requires that I get call execution back.
Only other thing that springs to $$anonymous$$d is something else interrupting while the CR has yielded. Like if the component it's on has been destroyed. Sounds like you're savvy enough to have thought of that but seemed worth mentioning as it's something that would impact on the CR method but not the while(!isDone) method.
@Bonfire Boy, oh crap. Guess I'm not that savvy. You're so right. That's what it is. Thanks for the help!
Your answer
Follow this Question
Related Questions
Wait for file download 1 Answer
How to imitate yield functionality like WWW class 0 Answers
Yield www within Gui Update() 0 Answers