Correct timeout on Connect Async Unity SDK

Hi, on Unity SDK I see that we could pass timeout on socket.ConnectAsync, and websocketadapter handle this by using cancellation token with timeout. but when I try on ios device, the time from I called connectAsync until the function throws and error took 60 seconds, even though I passed 10 secs on ConnectAync

tried also changing socket.idle_timeout_ms, write_timeout_ms, read_timeout_ms but still I don’t get the desired results.

code looks like this

    int timeout = 10;
    await socket.ConnectAsync(lastSession, false, timeout);
catch(Exception e)
    Log("Couldn't connect to the socket: {0}", e.Message); 

error and last callstack looks like this, after waiting for 60 secs

Couldn't connect to the socket mono-io-layer-error (60)
Log(String, Object[])
Nakama.WebSocketAdapter:ReadFrames(WebSocketReceiveResult, WebSocket, Byte[])
System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback, Object, Boolean)

how do I get a timeout with the correct val when I try first connecting socket?

@cloudAle This is a limitation with how the .NET code is cross-compiled to native code to be executed as part of the iOS Unity project. I’ll speak with @lugehorsam and see what we can do to improve or work around it but you should expect that network disconnects or cancellation timeouts will always be delayed due to the constraints of the operating system. Can you also let me know what Unity version you use for your project?

@novabyte thanks for replying, we use Unity LTS 2019.4.18f1

We’re okay if the timeout is delayed due to OS constrains if it was just a couple of seconds, but having to wait for a minute if the timeout set is just 10 seconds would potentially be a bad UX for our user, especially if it’s the first initialization on the splash screen.

We’ve seen a drop in our user retention when user re-enter to foreground after the game left on background because of reconnecting socket to the nakama server, as it won’t be showing “no internet modal” quickly

Thanks for the heads up, looking forward to the unity sdk improvements

@cloudAle The alternative is that you take advantage of the app lifecycle functions in Unity engine and programmatically disconnect the socket when the app is backgrounded and then connect the socket again when the app receives focus again.

@novabyte sorry if I’m missing something, but isn’t the act of reconnecting the socket after background would be the same process wise with the first socket connection on splash screen? (timeout would still be delayed nevertheless)