Socket from Unity SDK does not work properly

Hey, Nakama specialists! I have just started using Nakama (3.3.0) in one of the mobile projects and stumbled across a problem with a socket connection. Everything seems to be working fine on a target device, but when it comes to testing in the editor, things start to get strange.

I am using this code to keep track of the connected state of the socket:

var uri = new Uri("http://example.com:7350");

var client = new Client(uri.Scheme, uri.Host, uri.Port, "defaultkey");

var session = await client.AuthenticateDeviceAsync(SystemInfo.deviceUniqueIdentifier);

var socket = Socket.From(client, new WebSocketAdapter(3, 2));

socket.Connected += () =>
{
	UnityEngine.Debug.LogError("Connected");
};

socket.Closed += () =>
{
	UnityEngine.Debug.LogError("Closed");
};

await socket.ConnectAsync(session);

Then while connected I just unplug my laptop from the internet, count around 3 seconds and wait for the socket to disconnect. The problem is that nothing happens for around 15-20 seconds and only after that the message “Closed” appears. Have you got any idea of why that could happen?

Hi @igorshostak welcome to the forum.

Please can you let me know which version of Unity you’re using, as well as which version of the Nakama Server you’re using too?

Yeah, sure! I am developing stuff in Unity 2021.2.8f1 When it comes to the server, I am running nakama 3.10.0+ac9a4ccd

My best guess was that something does not “click” in the WebSocketAdapter. I have found out that the websocket itself gets closed with CloseSent when PingPongManager sets it after the connection is lost. But for some reason this state does not get propagated to the Closed event of WebSocketAdapter.

Hi @igorshostak,

We’ve done some testing both with the latest nakama-unity package (v3.3.0) and the previous version (v3.2.0) and we’re seeing the same behaviour as you described in both. So this appears to be a pre-existing bug and not something we’ve introduced with the latest patch. Please could you raise the issue against the nakama-dotnet repository (which is the base for the nakama-unity package and handles the Socket connections).

When raising the issue, if you could include the information you have gathered about where you believe the event is failing to be propagated as well as any specific reproduction steps that would be very much appreciated. We can then look into fixing this in a future release.