Mirror Server Instances on different ports

79 views Asked by At

So basically I just want to have multiple servers running on the same machine without needing to run the app several times. I've tried changing the port and start the server but I can only do that 1 time because if not I get this. enter image description here

This is my code so far. `[SerializeField] private int proximoPuerto = 1050; // Puerto inicial

[Header("Número máximo de intentos de puerto")]
[Tooltip("El número máximo de intentos de puerto antes de que se detenga la búsqueda de puerto")]
[SerializeField] private int maxPrtIntentos = 100; // Número máximo de intentos de puerto

private SyncDictionary<string, ushort> roomPorts = new SyncDictionary<string, ushort>();

[Header("El transport del netowrk manager")]
[Tooltip("Se necesitas el transport para poder cambiar el puerto y asi lanzar instancias del servidor en diferentes puertos")]
[SerializeField]private KcpTransport transport;

//Solo el servidor puede crear una sala ya que es el unico que esta configurado para abrir puertos
[Server]
public void CreateRoom(string roomName)
{
    if (!roomPorts.ContainsKey(roomName))
    {
        ushort availablePort = PortScanner.FindAvailablePort(proximoPuerto, maxPrtIntentos);
        if (availablePort != 0)
        {
            roomPorts.Add(roomName, availablePort);
            proximoPuerto = availablePort + 1;

            // Configura NetworkManager con el puerto de la sala
            transport.port = roomPorts[roomName];
            NetworkManager.singleton.StartServer();

            Debug.Log($"Room '{roomName}' creada en el puerto {roomPorts[roomName]}");
        }
        else
        {
            Debug.Log($"No hay puertos disponibles para '{roomName}'");
        }
    }
    else
    {
        Debug.Log($"Room '{roomName}' ya existe en el puerto {roomPorts[roomName]}");
    }
}`

Does anyone know another approach of running different server instances on different ports.

Thank you so much and sorry for my english.

0

There are 0 answers