How to TimeOut when Calling RPC in Unity?

I’ve written a RPC function that I call from the Unity SDK that simply pings the server to see if it’s online and the player is authenticated. Can somebody show me how to modify the following code to apply a 5 second time out ? Will the catch statement be called when it times out ?

try {
  await m_Client.RpcAsync(mysession, "ping", "");  
catch (Nakama.ApiResponseException ex) {
  // failed

@Spuddy There’s another function parameter you can pass into all the functions in the SDK which is named canceller and expects a CancellationToken. This is how you can provide a maximum time for the function to execute for.

var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
m_Client.RpcAsync(mysession, "ping", "", canceller: cts.Token);
// Will throw a TaskCancelledException if a timeout occurs.

All of this is standard .NET functionality and not Unity engine specific:

Hope this helps.

@gabriel probably good to add to Unity Guide in our docs plz.

For reference in case anyone lands on this topic, it is included in this section of the guide: Heroic Labs Documentation | Unity/.Net

But will try and make it more visible :+1:

I’ve tried what you said and created a CancellationToken with a time out of 2 seconds and also set a timeout of 10 seconds on the Nakama client but it’s taking around 75 seconds to throw the exception so I don’t understand what’s going on ? I’ve tested in the Unity Editor on a Mac but haven’t tried on a device yet

Update: It seems to time out after the expected 2 seconds on my iPad, just takes over a minute in the Unity Editor