Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Discussion Sending http requests in C# with Unity

Discussion in 'Scripting' started by pKallv, Aug 13, 2022.

  1. pKallv


    Mar 2, 2014
    I am working on a project with a database in the cloud and is reviewing my db access code structure.

    I have:

    • Auth2 check that generates a new token every 3550 sec if the game is online
    • I am currently accessing the DB via REST Api via coroutines
    I know that the api calls is suppose to be done asynchronous but with coroutines and the game requirement to wait for the api to complete before continuing the code becomes kind of ugly given that I then need to move to the next step. With synchronous db access I can also return the result directly from the function to the caller.

    I am thinking of doing it this way in a normal function:

    Code (CSharp):
    2. string PlayersGET2(string _GetParameter) {...
    4. var request = www.SendWebRequest();
    5. while (!request.isDone) { >> Insert a timeout timer here << }

    Code (CSharp):
    2. IEnumerator PlayersGET(string _GetParameter) {...
    4. yield return www.SendWebRequest();
    By using a timeout timer I can allow the game to wait for a maximum of like 2-3 seconds and then time out and take appropriate action.

    Can someone nice reflect disadvantages/advantages of doing it synchronous vs async in my scenario?
  2. R1PFake


    Aug 7, 2015
    Did you test the code, because most Unity "async" operations can't even be awaited synchronous in a while loop like that, because they still require the "main loop" to continue in order to actually finish, so there is a good chance that your while loop will never actually finish the request and always "timeout" with a failed request.

    But even if it works, I personally would consider this very ugly, but it's your code you can do what you want, as long as it works for your. It could still fail if your timeout is too low or cause frame drops if you set it to high etc, I would just properly wait until it's done instead of trying to "guess" it.

    What kind of error handling do you plan for your timeout? Always assume that the request failed? Assume that the connection failed? Try again later and hope that the request finishes before the timeout next time? None of these options are good.

    Also 2-3 seconds timeouts are way too high for a game.
    Im not sure what kind of game you make, but I assure no player would like to freeze 2-3 seconds while you wait for your timeout. People hate these kind of "freezes", even in "normal" apps.

    Like you said, you can use a Coroutine, the event callback of the request or wrap the request inside a task and await it and I would consider all of these options better than a the blocking while check

    With the coroutine you can write all the logic in order and put as many yields in there as you need and use the data directly in the line after the request is done or call some other methods / callbacks. It reads almost like a normal method, so Im not really sure which part about that you consider ugly. Maybe you can give more details why you consider it ugly?
    Last edited: Aug 13, 2022
    pKallv likes this.