While developing a game in Unity, I’m experiencing significant performance lag during network disconnection and reconnection processes. Upon investigation with Unity’s Profiler, I’ve identified that the high latency is occurring in a specific section of the code.
How should I handle this?
- Versions: {client library version 3.10.1}
- Server Framework Runtime language {TS}
Media:
@zhuguiqing It would be great if you could share more context to help identify the root cause:
- What kind of game have you developed? Which Nakama socket features do you use?
- Is there a reason you’re on the Nakama Unity 3.10.1 release? Why have you not updated?
- Do you use the authoritative match handler engine in Nakama with TypeScript? What does the VM configuration parameters look like which you use?
- What does your specific Unity/C# disconnect and reconnect logic look like?
Let me answer questions 2 and 4.
For question 2: We initially started using Nakama Unity 3.10.1 and haven’t felt the need to upgrade since then. The version has been stable and meets our current requirements. We’ve been using this version consistently across our development process without encountering any issues that would necessitate an upgrade.
For question 4: Our reconnection mechanism is triggered by ReceivedError events or when specific exceptions occur, such as TaskCanceledException, InvalidHttpResponseCodeException, and ApiResponseException. When these events or exceptions are detected, the system automatically initiates a reconnection process to restore the connection with the server. This helps maintain a stable connection even in cases of temporary network issues or server unavailability.
Then, the reconnection logic is roughly as follows:
string authToken = PlayerPrefs.GetString(AuthTokenKey, null);
string refreshToken = PlayerPrefs.GetString(RefreshTokenKey, null);
bool sessionExpired = false;
try
{
Session = Nakama.Session.Restore(authToken, refreshToken);
if (Session.HasRefreshExpired(DateTime.UtcNow.AddHours(TokenExpiredHours)))
{
sessionExpired = true;
}
else if (Session.HasExpired(DateTime.UtcNow.AddHours(TokenRefreshHours)))
{
Session = await Client.SessionRefreshAsync(Session);
}
}
catch (Exception e)
{
Debug.LogError($"Session is expired : {e}");
sessionExpired = true;
}
if (sessionExpired)
{
// get a new refresh token
string guid = DeviceInfo.UniqueGuiD;
Session = await Client.AuthenticateAsync(Client, guid);
}
await _socket.ConnectAsync(Session);
Using the same client, there is no lag when connecting to an HTTP server, but there is significant lag when connecting to an HTTPS server. What are the differences in how these two protocols are handled?