heroiclabs / nakama-unity

Unity client for Nakama server.
https://heroiclabs.com/docs/unity-client-guide
Other
407 stars 75 forks source link

SocketException: mono-io-layer-error (61) on iOS device for AuthenticateDeviceAsync #152

Closed MadGeorge closed 1 year ago

MadGeorge commented 1 year ago

Hello. Following basic setup instructions from the documentation facing crash iOS device.

SocketException: mono-io-layer-error (61)
  at System.Net.Sockets.SocketAsyncResult.CheckIfThrowDelayedException () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00000] in <00000000000000000000000000000000>:0 
--- End of stack trace from previous location where exception was thrown ---

  at System.Net.WebConnection.Connect (System.Net.WebOperation operation, System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction (System.Action action, System.Boolean allowInlining, System.Threading.Tasks.Task& currentTask) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.Tasks.Task.FinishContinuations () [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.Tasks.Task.TrySetException (System.Object exceptionObject) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00000] in <00000000000000000000000000000000>:0 
Rethrow as WebException: Error: ConnectFailure (mono-io-layer-error (61))
  at System.Net.WebConnection.Connect (System.Net.WebOperation operation, System.Threading.CancellationToken cancellationToken) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <00000000000000000000000000000000>:0 
...

Full log on GDrive

This code works just fine in the Unity Editor, but on the iOS device it crashes on AuthenticateDeviceAsync.

using Satori;
using UnityEngine;
public class Connection : MonoBehaviour
{
    private Nakama.Session _session;
    private readonly Nakama.Client _client = new ("http", "127.0.0.1", 7350, "defaultkey");

    private async void Start()
    {
        var deviceId = PlayerPrefs.GetString("deviceId", SystemInfo.deviceUniqueIdentifier);

        // If the device identifier is invalid then let's generate a unique one.
        if (deviceId == SystemInfo.unsupportedIdentifier)
        {
            deviceId = System.Guid.NewGuid().ToString();
        }

        Debug.Log($"Request Authentication for Device ID: {deviceId}");

        // Save the user's device ID to PlayerPrefs so it can be retrieved during a later play session for re-authenticating.
        PlayerPrefs.SetString("deviceId", deviceId);

        // Authenticate with the Nakama server using Device Authentication.
        try
        {
            _session = await _client.AuthenticateDeviceAsync(deviceId) as Nakama.Session;
            Debug.Log($"Authenticated with Device ID: {deviceId}");
        }
        catch(ApiResponseException ex)
        {
            Debug.LogFormat($"Error authenticating with Device ID: {deviceId}:", ex.Message);
        }
    }
}

/** 
Logs in the Editor:
Request Authentication for Device ID: 4331119F-F031-...
uthenticated with Device ID: 4331119F-F031-...

Logs on iOS Device:
Request Authentication for Device ID: F8A7189F-6123-...
Stacktrace is not supported on this platform.

SocketException: mono-io-layer-error (61)

Nakama Unity asset: 3.6.0 Device specs: iPhone X iOS 15.6.1 Unity: 2022.2.7f1 and 2021.3.9f1 Other dependencies: Literally no. Just empty 2D project from Unity template.


The question: The reason of this crash is looks like invalid host address since it runs on my mac and iOS can not reach 127.0.0.1. But why the error is so cryptic and scary?

lugehorsam commented 1 year ago

Hey @MadGeorge since the error comes from Mono we can't really speak to why they chose that wording.

We have a pending task to wrap some of these cryptic error messages into something more friendly for error messages.

Since that's tracked separately and not an issue per se with the SDK, I'm going to close this issue down.