Checking if the network is down

Hey,

I realize this is common question and I have read the previous posts about it but they don’t quite answer what I’m wondering about.

If the player goes offline you can detect it by a try/catch on a nakama call, but my question is what would be the best approach after that. Here’s what I do now:

  1. Try/catch to get any network error, whenever a nakama call is being made
  2. Push a “connection error”-popup on player (potentially forcing the load screen), the player can press “Try again” to reconnect.
  3. The reconnect will authenticate/restore the session and if the socket is reporting as disconnected it’ll connect the socket.

But that won’t really work as you can restore the session while being offline and the socket reports as connected even if you disconnect.

So a couple alternatives I’ve come up with would be to either do a new authentication call to catch it (so never restore after a network error), or do some Ping RPC call to catch it, and then once again show the popup.

Does anyone have a better practice for this?

Thanks!

Hey @nixarn, your initial approach sounds good to me. When you bring up the caveats with it, you mention:

(1) you can restore the session while being offline

You don’t need to check the session for any connected status. You’ve already done it through the try...catch.

(2) the socket reports as connected even if you disconnect.

This sounds like a bug. Can you file an issue on our dotnet client with some sample code? GitHub - heroiclabs/nakama-dotnet: .NET client for Nakama server written in C#.

1 Like

You don’t need to check the session for any connected status. You’ve already done it through the try…catch.

Yeah that would work but only if you do the authenticate call, and not the restore call.

And about the bug, I wonder if it is a bug or “as intended”. The steps to reproduce it basically:

  1. Create socket
  2. Check socket: IsConnected = false
  3. Connect socket
  4. Check socket: IsConnected = true
  5. Disconnect your device from the Internet (computer in this case, running in Unity)
  6. Check socket: IsConnected = true

However, if you then reconnect to the internet, the socket will work without a new “connect” call. So maybe this is intended behaviour.

session.Restore is just meant to reconstitute the JWT stored in the client’s cache into a human-readable object. It isn’t intended to detect whether the client has a connection with the server, or “refresh” the current session. In order to “refresh” the session (i.e., obtain a new JWT from the server), use Authenticate....

We’re going to be releasing a new version of the Unity library next week that automates this for you.

1 Like

Yeah, aware of the restore, my point was just that if I was able to restore a session then I’d need some other RPC request to detect network errors.

Looking forward to the new Unity library! Thanks!

Any news on the updated Unity library? Thanks!

Any news on the updated Unity library? Thanks!

@nixarn We’ve just released the .NET client SDK update and the Unity client SDK package release will follow tomorrow. In the meantime have a look at the release notes for the .NET code as its where most of the changes come from and let me know if you have any questions.

1 Like

@nixarn we just released the Unity client on Github. It will be out in the the Asset Store when it’s approved my Unity. They told us ~2 days. Release v2.9.0 · heroiclabs/nakama-unity · GitHub

This will now handle automated refreshing of sessions. Lets see if this helps you and if you still hit issues or have questions we can revisit for sure.