1

I'm making a videogame and I need clients and server to communicate (in Unity).

The message from the server needs to be converted to a specific type based on the request: for example, the registration process will return just a bool in case of success or not; the login process must return a lot of information in a dictionary for the user who logged in (level, experience, game progress, etc.)

In java I used to do something like this:

net().post(url.toString(), safeUrl, new Callback<String>()
{
    public void onFailure(Throwable t)
    {
        // do something to handle the failure...
    }

    public void onSuccess(String json)
    {
        // do something...
    }
});

Basically the net().post was sending a post http request to the server, and once it was done the server was calling the correct method.

I read that C# can't do this, but that delegates can achieve something similar, do you know how? so basically I just send to the post request a method as parameter and handle it? Also is it possible to use generic types to handle the different types of requests?

Or Is there a better way to do this?

user3071284
  • 6,955
  • 6
  • 43
  • 57
Epilogue
  • 63
  • 1
  • 8

2 Answers2

1

No, there is no direct equivalent to that in c#, however you may use delegates, e.g. as follows:

class Callback {
    private readonly Action<Exception> onFailure;
    private readonly Action<string> onSuccess;

    public CallBack(Action<Exception> onFailure, Action<string> onSuccess) {
        this.onFailure = onFailure;
        this.onSuccess = onSuccess;
    }

    public void FireFailure(Exception e) {this.OnFailure(e);}
    public void FireSuccess(string json) {this.OnSuccess(json);}
}

Now it is up to your net().post-method to fire the approriate events.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
1

You have two alternatives here.

You can use Action<string> to pass in anonymous callbacks to your method.

Or...

you dive into the async/await world, which is the recommended approach.


Using anonymous delegates

Your Post signature would look like this:

public void Post(string url, string safeUrl, Action<string> onSuccess, Action<Exception> onFailure);

Notice that you must have an argument for each callback result.

Inside your Post method, you should call the callbacks when done.

From the client (or caller), you would call it like this:

Post(url, safeUrl, response =>
{
   // Success callback body
}, exception =>
{
   // Failure callback body
});

Using async/await (I don't know if this approach is supported in Unity)

Your Post signature would look like this:

public async Task<string> Post(string url, string safeUrl);

Inside your Post method, when sending the request you should use a method that support async/await, such as HttpClient.PostAsync():

// This is just an example
public async Task<string> Post(string url, string safeUrl)
{
     HttpResponseMessage response = await httpClient.PostAsync(url, null);
     string content = await response.Content.ReadAsStringAsync();
     return content;
}

You would call it like this:

Task<string> fetchingResponse = Post(url, safeUrl);

// You can keep doing things while you wait for the response ...

try
{
    string response = await fetchingResponse;
    // Success logic here
}
catch(Exception ex)
{
    // Failure logic here
}

Notice that there are no callbacks.

Also, keep in mind that your caller method (i.e. the method that is executing Post) should also be anotated with async and should also return a Task or Task<T> instance.

Community
  • 1
  • 1
Matias Cicero
  • 25,439
  • 13
  • 82
  • 154