Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to Stop Allocated server?

Discussion in 'Game Server Hosting' started by b4guw1x, Sep 7, 2023.

  1. b4guw1x

    b4guw1x

    Joined:
    Apr 16, 2020
    Posts:
    12
    Hey everyone. I'm using Unity Multiplay system with matchmaker. Everything works just fine but when i disconnect all of the clients, servers stil remain allocated like this until i click on the server and click stop server.

    Server List.png


    Is it normal? Will it cost any money if there is no client in it? Also how can i stop server via code? In server
    Code (CSharp):
    1.     public NetworkServer(NetworkManager networkManager)
    2.     {
    3.         _networkManager = networkManager;
    4.         _networkManager.OnServerStarted += OnNetworkReady;
    5.     }
    6.  
    7.     private void OnNetworkReady()
    8.     {
    9.         _networkManager.OnClientConnectedCallback += OnClientDisconnect;
    10.     }
    11.  
    12.     private void OnClientDisconnect(ulong clientID)
    13.     {
    14.         if (!_networkManager) return;
    15.    
    16.         if (_networkManager.ConnectedClients.Count == 0)
    17.         {
    18.             Dispose();
    19.         }
    20.     }
    21.  
    22.     public void Dispose()
    23.     {
    24.          if(!_networkManager) return;
    25.  
    26.         _networkManager.OnClientDisconnectCallback -= OnClientDisconnect;
    27.         _networkManager.OnServerStarted -= OnNetworkReady;
    28.  
    29.          if (_networkManager.IsListening)
    30.          {
    31.                _networkManager.Shutdown();
    32.          }
    33.      }
    34.  
    35.  
    36.  
    i got this but it seems not working.
     
  2. b4guw1x

    b4guw1x

    Joined:
    Apr 16, 2020
    Posts:
    12
    I think i solved it. Firstly, yes it's a problem since match maker needs deallocated servers to create new matches. What i did is, if there is no connected client, i will close the application. According to documentation, https://docs.unity.com/ugs/en-us/manual/game-server-hosting/manual/concepts/deallocations server hosting system is detecting the exit signal and deallocate server automaticly. But make sure there is no error when you do that.

    Here is my ServerManager code.


    Code (CSharp):
    1.     #region Components
    2.     private NetworkManager _networkManager;
    3.     #endregion
    4.  
    5.     #region Variables
    6.  
    7.     private List<ulong> _connectedClients = new();
    8.  
    9.     #endregion
    10.  
    11.  
    12.     public NetworkServer(NetworkManager networkManager)
    13.     {
    14.         _networkManager = networkManager;
    15.         _networkManager.OnServerStarted += OnNetworkReady;
    16.     }
    17.  
    18.     private void OnNetworkReady()
    19.     {
    20.         _networkManager.OnClientConnectedCallback += OnClientConnected;
    21.         _networkManager.OnClientDisconnectCallback += OnClientDisconnect;
    22.     }
    23.  
    24.     private void OnClientConnected(ulong clientID)
    25.     {
    26.         _connectedClients.Add(clientID);
    27.         NetworkLog.LogInfoServer($"{clientID} connected. Current client count : {_connectedClients.Count}");
    28.     }
    29.  
    30.     private void OnClientDisconnect(ulong clientID)
    31.     {
    32.         if (!_networkManager || !_connectedClients.Contains(clientID)) return;
    33.  
    34.         _connectedClients.Remove(clientID);
    35.         NetworkLog.LogInfoServer($"{clientID} disconnected. Current client count : {_connectedClients.Count}");
    36.  
    37.         if (_connectedClients.Count == 0)
    38.         {
    39.             Application.Quit(0);
    40.         }
    41.     }
    And here is the result. (They are no crash, unexpected exits etc. Just close with no problem). Starting server. Exiting and add the server to available pool

    upload_2023-9-8_14-55-18.png
     
    Last edited: Sep 8, 2023
  3. jackw_unity

    jackw_unity

    Unity Technologies

    Joined:
    Oct 12, 2022
    Posts:
    11
    Hi @b4guw1x great stuff, glad to see our docs came in handy.

    We generally recommend to wait ~1 minute in most scenarios for clients and then if your client count does not increase then exit.

    This will also help to account for situations where a server has become started and allocated but something went wrong.
    An example would be if a network issue occurred and players did not end up routed onto the server.

    We do have 'allocation timeout' function which is an auto-heal type feature to avoid capacity being perpetually online and allocated, but this is a final resort and the default timeout for it is one hour.