Hello All!
I have a WebGL game that I am transitioning over to using the new Multiplayer Services package for.
I tried using the out of the box session tools and they worked perfectly for udp, but it kept throwing a “lobby not found error” with the websockets when I selected to use it on the NetworkManager object.
I tried just using the relay SDK but it kept saying the join code was not found, even after doing the .substring(0,6) trick.
Any thoughts? I’d also be interested in any tutorials for the new multiplayer services package that use Websockets for WebGl.
The session system doesn’t support WebGL (WSS) yet, it’s on the teams backlog. So you’ll need to use the standalone namespaces for Relay and Lobby. Documentation for those services remains relevant with some caveats outlined here:
Got it. Thank you very much! I put my final code below just in case it can help anybody else. The only things you would need to change would be the JoinCode and GameCode variables to whatever you are storing the code in.
public async void HostGame()
{
await UnityServices.InitializeAsync();
if (!AuthenticationService.Instance.IsSignedIn)
{
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
Allocation allocation = await RelayService.Instance.CreateAllocationAsync(3);
var serverData = AllocationUtils.ToRelayServerData(allocation, "wss");
NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(serverData);
var gameCodeStr = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
gameCode.text = NetworkManager.Singleton.StartHost() ? gameCodeStr : null;
Debug.Log(gameCodeStr);
}
public async void JoinGame() {
await UnityServices.InitializeAsync();
if (!AuthenticationService.Instance.IsSignedIn)
{
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
string joinStr = joinCode.text.Substring(0, 6);
var joinAllocation = await RelayService.Instance.JoinAllocationAsync(joinCode: joinStr);
var serverData = AllocationUtils.ToRelayServerData(joinAllocation, "wss");
NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(serverData);
NetworkManager.Singleton.StartClient();
}
Trick? I’m amazed at how crappy solutions like this are spread these days.
What’s the intent and purpose for this? I suppose the string may have extra spaces. In that case it’s much safer to use as it does not depend on the string’s length (could be less than 6 chars in which case the above throws an exception):
var joinStr = joinCode.text.Trim();
In all other cases neither “trick” will lead to the desired results.
Following these guides for distributed authority, sessions still don’t appear to work in WebGL.
Additionally, it seems that the guide is outdated, as step 4 in the “Netcode for GameObjects setup” section of the first guide results in distributed authority not working at all. “DistributedAuthorityTransport” must be selected instead of “UnityTransport”, otherwise NGO will create a DistributedAuthorityTransport component on its own, and without “Use Web Sockets” being checked in this new component, MultiplayerServices fails to connect to a relay when attempting to use CreateOrJoinSessionAsync.
(Using Unity 6000.0.37f1, Multiplayer Services 1.1.1, NGO 2.2.0, and Unity Transport 2.4.0)