Hi, I’m completely new to Netcode. I want to build to WebGL and have it talk to my Dedicated Server build. So, since UDP is not supported in WebGL, I must use WebSockets.
If I understand right, in order to use WebSockets, I need to do a custom
INetworkStreamDriverConstructor
and do some connection setup manually (code copied from Question - Custom Transport - Unity Forum )
So far, when I enter playmode in the editor, in the ServerWorld there is a connection entity with a NetworkId. But, in the ClientWorld, there is no entity with a NetworkId. I see in the docs NetworkId is "Added automatically when connection is complete"
.
So, after a few frames, I am getting an error:
Resetting event queue with pending events (Count=1, ConnectionID=0) Listening: 0
What I am wondering: is there a “here is your NetworkId, client” event being missed by the client, because its queue is reset while before the event is processed? Why is the queue being reset? Why isn’t the event being processed? Maybe I am taking completely the wrong approach.
using Unity.Entities;
using Unity.NetCode;
using Unity.Networking.Transport;
using UnityEngine;
public class MyCustomNetworkBootstrap : ClientServerBootstrap
{
override public bool Initialize(string defaultWorldName)
{
NetworkStreamReceiveSystem.DriverConstructor = new MyCustomDriverConstructor();
return base.Initialize(defaultWorldName);
}
}
public struct MyCustomDriverConstructor : INetworkStreamDriverConstructor
{
ushort Port => 7777;
public void CreateClientDriver(World world, ref NetworkDriverStore store, NetDebug netDebug)
{
var instance = DefaultDriverBuilder.CreateClientNetworkDriver(new WebSocketNetworkInterface());
store.RegisterDriver(TransportType.Socket, instance);
var endpoint = NetworkEndpoint.LoopbackIpv4.WithPort(Port);
var connectResult = instance.driver.Connect(endpoint);
if (connectResult.IsCreated != true)
{
Debug.LogError($"Failed to connect to port {Port}.");
return;
}
}
public void CreateServerDriver(World world, ref NetworkDriverStore store, NetDebug netDebug)
{
var instance = DefaultDriverBuilder.CreateServerNetworkDriver(new WebSocketNetworkInterface());
store.RegisterDriver(TransportType.Socket, instance);
var endpoint = NetworkEndpoint.AnyIpv4.WithPort(Port);
var bindResult = instance.driver.Bind(endpoint);
if (bindResult != 0)
{
Debug.LogError($"Failed to bind to port {Port}.");
return;
}
var listenResult = instance.driver.Listen();
if (listenResult != 0)
{
Debug.LogError($"Failed to listen on port {Port}.");
return;
}
}
}