0
\$\begingroup\$

so I'm trying to make a internet connection with c# using unity, and what I want to do is make the client to connect to the site, and load the data.

but there are cases there's lag, or weak connection, etc. that prevents you from getting connected, and I want the client to stop attempting to connect and just return connectionError or something like that after certain amount of time.

my code for this so far:

void HTTPRequest_TimeOut ( )
{

    string url = "serverAddress";

    HTTPRequest request = new HTTPRequest ( new Uri ( url ), (req, resp ) =>
        {
            switch ( req.State )
            {
                case HTTPRequestStates.Finished:
                    // utf-8 인코딩.
                    byte [] Bytes_For_Encoding = Encoding.UTF8.GetBytes ( resp.DataAsText );
                    string Encoded_String = Convert.ToBase64String ( Bytes_For_Encoding );

                    // utf-8 디코딩.
                    byte [] Decoded_Bytes = Convert.FromBase64String ( Encoded_String );
                    string Decoded_String = Encoding.UTF8.GetString ( Decoded_Bytes );

                    JsonData getdata = JsonMapper.ToObject ( Decoded_String );
                    DB_INPUT_test ( getdata, "items" );

                    break;

                case HTTPRequestStates.Error:
                    Debug.LogError ( "Request Finished with Error! " );
                    break;

                case HTTPRequestStates.Aborted:
                    Debug.LogWarning ( "Error : Request Aborted!" );
                    break;

                case HTTPRequestStates.ConnectionTimedOut:
                    Debug.LogError ( "Error : Connection Timed Out!" );
                    break;

                case HTTPRequestStates.TimedOut:
                    Debug.LogError ( "Error : Processing the request Timed Out!" );
                    break;
            }
        } );


    request.Timeout = TimeSpan.FromSeconds ( 100 );
    request.DisableCache = true;
    request.Send ( );
}

at first I thought just changing request.Timeout would do the trick, I guess not?

currently if the connection is not strong(making mobile game) it just kinda "hangs" there, waiting for anything to load up.

\$\endgroup\$
2
  • \$\begingroup\$ Use Try catch so when the request is time out a tmeout exception will be thrown \$\endgroup\$ Commented Jun 4, 2018 at 13:14
  • \$\begingroup\$ If you are calling this from update, add in a timer using Time.delta time every time you hit a case other than .Finished. \$\endgroup\$ Commented Jun 4, 2018 at 14:55

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.