Problem
I registered a method on ‘ISocket.Connected’ to register the server connection completion callback, but the result was not as I expected. The callback was called, but ‘ISocket.IsConnected’ returned false. Why ‘ISocket.Connected’ not working? The callback to ‘ISocket.Closed’ works fine.
My unity editor version is 2020.3.25f1, and nakama sdk imported the latest version from unity asset store.
Code
private async UniTask ConnectAsync()
{
string authToken = PlayerPrefs.GetString("nakama.session");
if (!string.IsNullOrWhiteSpace(authToken))
{
ISession session = Nakama.Session.Restore(authToken);
if (!session.IsExpired)
{
_session = session;
}
}
if (_session == null)
{
string deviceId;
if (PlayerPrefs.HasKey("nakama.deviceUniqueIdentifier"))
{
deviceId = PlayerPrefs.GetString("nakama.deviceUniqueIdentifier");
}
else
{
deviceId = SystemInfo.deviceUniqueIdentifier;
if (deviceId == SystemInfo.unsupportedIdentifier)
{
deviceId = System.Guid.NewGuid().ToString();
}
PlayerPrefs.SetString("nakama.deviceUniqueIdentifier", deviceId);
}
_session = await _client.AuthenticateDeviceAsync(deviceId);
PlayerPrefs.SetString("nakama.session", _session.AuthToken);
}
_socket = _client.NewSocket();
//Register
_socket.Connected += DoWhenConnected;
_socket.Closed += DoWhenDisconnected;
await _socket.ConnectAsync(_session);
}
//connected callback
private void DoWhenConnected()
{
Debug.Log("Nakama server connecting?: " + _socket.IsConnecting);
Debug.Log("Nakama server already connected?: " + _socket.IsConnected);
}
private void DoWhenDisconnected()
{
Debug.Log("Nakama server disconnected?" + !_socket.IsConnected);
}