Connection Helper

This is a component that helps you connect to the server without writing your own code. It is designed to automatically reconnect if the connection is lost. This class is derived from Singleton<T>, so its lifecycle runs throughout the game until you destroy it.

abstract class ConnectionHelper<T> : Singleton<T>

There is a class in the framework that uses ConnectionHelper. This is the master server connection class. It uses a global connection, which in turn is used by all the main systems of this framework.

class ClientToMasterConnector : ConnectionHelper<ClientToMasterConnector>

Awake()

The Awake method of the ClientToMasterConnector class searches for command-line arguments that help set the IP address and port of the master server.

protected override void Awake()
{
    base.Awake();

    // If master IP is provided via cmd arguments
    if (Mst.Args.IsProvided(Mst.Args.Names.MasterIp))
    {
        serverIp = Mst.Args.MasterIp;
    }

    // If master port is provided via cmd arguments
    if (Mst.Args.IsProvided(Mst.Args.Names.MasterPort))
    {
        serverPort = Mst.Args.MasterPort;
    }
}

ConnectionFactory()

The ConnectionFactory method creates a new instance of the connection. To create a new connection instance, you must override the ConnectionFactory method in your class derived from ConnectionHelper, as shown in the example below.

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

SetIpAddress()

The SetIpAddress method sets the IP address that the client should connect to.

public void SetIpAddress(string serverIp);

SetPort()

The SetPort method sets the port that the client will use to connect to the server

public void SetPort(int serverPort);

StartConnection()

The StartConnection method starts a connection with the specified parameters, such as the server's IP address, server port, and the number of attempts to connect to the server.

// Start connection using default parameters
public void StartConnection();

// Start connection with the given number of attempts
public void StartConnection(int numberOfAttempts);

// Start connection with the given IP, port and number of attempts
public void StartConnection(string serverIp, int serverPort, int numberOfAttempts = 5);

Connection Events

// Invokes when connection established
public UnityEvent OnConnectedEvent;

// Invokes when client disconnected
public UnityEvent OnDisconnectedEvent;