- Home /
My game freezes whenever i use a function which involves http?
My game freezes whenever i use any fucntion which includes any http calls. I mean the game is waiting for the data to get completed eveything else stops in the mean time. Like the sprite animations,game time,etc..
Example:
public PendingGamesByUser GetPendingGamesByUser(string userName)
{
XmlNode xmlNodes = null;
PendingGamesByUser pendingGamesByUser = new PendingGamesByUser();
List<PendingGamesYourTurn> pendingGamesYourTurns = new List<PendingGamesYourTurn>();
List<PendingGamesTheirTurn> pendingGamesTheirTurns = new List<PendingGamesTheirTurn>();
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(string.Concat("http://www.poquere.com/gotchha/getPendingGames.php?name=", userName));
HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
string end = (new StreamReader(response.GetResponseStream())).ReadToEnd();
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(end);
string empty = string.Empty;
foreach (XmlNode xmlNodes in xmlDocument.SelectNodes("//response/data/yourturn"))
{
empty = xmlNodes.InnerText;
PendingGamesYourTurn pendingGamesYourTurn = new PendingGamesYourTurn();
pendingGamesYourTurns.Add(JsonConvert.DeserializeObject<PendingGamesYourTurn>(empty));
}
foreach (XmlNode xmlNodes1 in xmlDocument.SelectNodes("//response/data/theirturn"))
{
empty = xmlNodes1.InnerText;
PendingGamesTheirTurn pendingGamesTheirTurn = new PendingGamesTheirTurn();
pendingGamesTheirTurns.Add(JsonConvert.DeserializeObject<PendingGamesTheirTurn>(empty));
}
pendingGamesByUser.PendingYourTurn = pendingGamesYourTurns.ToArray();
pendingGamesByUser.PendingTheirTurn = pendingGamesTheirTurns.ToArray();
return pendingGamesByUser;
}
When i call the above function like below
pendinggame = GetPendingGamesByUser("kannan21jan");
it lags right there. How to solve this?
What platform are you developing for? i think ios has issues with HttpWebRequest, why not use the www class ins$$anonymous$$d?
Try finding out the cause for the lag like the client could not connect to the server or server does not respond leaving a timeout error. Or it might be some other network related issue.
If it is not network related issue then you might want to look at other issues like code using Profiler (Unity Pro only) or manual debugging.
@labdern:I am using a dll for the webservice. And it is made of that http. So i pretty much can't do anything abt it. And yes i'm doing this for iOS.
A coroutine will halt the main thread. If you want it to run concurrently, use a thread.
Your answer
Follow this Question
Related Questions
Nested coroutine handling 0 Answers
Make a function that can return string or IEnumerator 2 Answers
Unity 5.0 HTTPS/SSL Certificate Improvements? 0 Answers
POST to HTTP not working 2 Answers
Generating Objects at High Speeds 0 Answers