Create New Connection Helper

This framework already contains a working component of the connection helper. This is a ClientToMasterConnector. However, there are situations when you need to use multiple connection helpers to connect to another server in the same scene. In this case, the use of the ClientToMasterConnector will not help. The point is that ClientToMasterConnector is derived from the Singleton class and therefore cannot have a second instance copy in the scene. Creating your own connection helper will help you.

Let's imagine that we need a connection helper that will communicate with a microservice that processes data remotely. Let's call it the DataManagerServiceConnector. Let's create a new class with this name as shown in the example below.

public class DataManagerServiceConnector : MonoBehaviour
{
   
}

We derive this class from ConnectionHelper and pass our DataManagerServiceConnector class as a type, as shown below.

public class DataManagerServiceConnector : ConnectionHelper<DataManagerServiceConnector>
{

}

Next, we need to override the ConnectionFactory method so that it returns a new instance of our client connection, as shown below.

public class DataManagerServiceConnector : ConnectionHelper<DataManagerServiceConnector>
{
    protected override IClientSocket ConnectionFactory()
    {
        return Mst.Create.ClientSocket();
    }
}

If you need to perform additional initialization of the parameters of your new connection helper, you should also overload the Awake method.

public class DataManagerServiceConnector : ConnectionHelper<DataManagerServiceConnector>
{
    protected override void Awake()
    {
        base.Awake();

        // Initialize something else
    }

    protected override IClientSocket ConnectionFactory()
    {
        return Mst.Create.ClientSocket();
    }
}