We are trying to launch a Unity WebGL client connecting to Nakama and running into a socket creation error (like other users on here). This isn’t our first time. We successfully launched a WebGL game in the past and thought we had the code nailed down. But for whatever reason Nakama doesn’t like what we are doing now.
For reference we are using Unity 6000.r32 and Nakama 3.22.0 for local dev, but also seeing this behavior on an older version we used successfully in the past on Azure (v3.17.0). This leads us to believe something in the Nakama-Unity client has changed. Our successful game used client v3.3.0. Our latest where the problem is occurring is using 3.14.0 (updated from 3.13.0 after encountering this error).
Here are the basics of our code:
public async Task<Result> Connect()
{
PlayerPrefs.DeleteKey("nakama.token");
string scheme = NetworkConstants.SCHEME_HTTP;
if (_endpoint.IsSecure)
{
scheme = NetworkConstants.SCHEME_HTTPS;
}
#if UNITY_WEBGL && !UNITY_EDITOR
ConnectionManager.Client = new Client(scheme, _endpoint.Address, _endpoint.Port, _endpoint.ServerKey, UnityWebRequestAdapter.Instance);
Debug.Log($"{ConnectionManager.ServiceName}: I'm a WebGL build!");
#else
ConnectionManager.Client = new Client(scheme, _endpoint.Address, _endpoint.Port, _endpoint.ServerKey);
Debug.Log($"{ConnectionManager.ServiceName}: I'm not a WebGL build");
#endif
Debug.Log($"{ConnectionManager.ServiceName}: Attempting connection to {ConnectionManager.Client.Scheme}://{ConnectionManager.Client.Host}:{ConnectionManager.Client.Port} with key {ConnectionManager.Client.ServerKey}");
var result = await AuthenticateSession();
if (result.Success) { CreateSocketOnMain(); }
return result;
}
private void CreateSocketOnMain()
{
// set so Nakama socket event handlers run in Unity's main thread.
ConnectionManager.MainSocket = ConnectionManager.Client.NewSocket(true);
}
When we run this code in the Unity editor for local testing using Windows or Mac machines, it works exactly as expected.
But when we use “Build and Run” to launch a WebGL build and connect to the either Nakama instance (docker for local dev 3.22.0 or Azure instance 3.17.0) we see the same socket creation error.
We looked through the forum and found this explanation: WebGL error
We are not directly including UnityWebSocketBridge.jslib in our build, and if its pulled in by changing our build profile to WebGL then we don’t have direct access to modify it.
We’ve also searched through our source code and there is no place where the Pointer_stringify method is being used. We are not using any third-party assets with compiled DLLs.
Any help would be greatly appreciated.
-Allen