Search Unity

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:
    40
    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:
    40
    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
    MohamedBadawy likes this.
  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.
     
    b4guw1x likes this.
  4. ANLevant

    ANLevant

    Joined:
    Jun 10, 2014
    Posts:
    28
    I am having this very same issue but for some reason, I tried implementing your code as is and it doesn't detect client disconnection when I close the app / stop the editor. As a result server stays allocated forever until manually deallocated, incurrig in costs. I'm trying to find a solution for this...
     
  5. b4guw1x

    b4guw1x

    Joined:
    Apr 16, 2020
    Posts:
    40
    Hello. I know it's been a while but i just saw it. On my game, i try to shutdown network manager on players OnNetworkDespawn methode. And with that, each player disconnect correctly. Can you try that?
     
  6. razzaq94

    razzaq94

    Joined:
    Jul 19, 2016
    Posts:
    4
    Hi i am having the same issue Application.Quit(0) is not working for me, and for some reason non of the callbacks are running on the server side like client connection and disconnection callbacks, and i used to be able to check server logs but right now i am unable to check them for some reason, im in a pickle plz help.

    i diched all call backs and i used Invoke repeting which constantly checks weather NetworkManager.Singleton.connectedclients.count == 0, if it is 0 then Application.Quit(0) is called but its still not working, server is not shutting down, can you give any insight on this.
     
  7. b4guw1x

    b4guw1x

    Joined:
    Apr 16, 2020
    Posts:
    40
    Sorry to hear that. Firstly, i would recommend you to use your own connectedClients list. Add player to list OnClientConnected callback, and remove it OnClientDisconnected callback. Then check this list. Also make sure that you are starting to listening this events when your server is started as i implemented


    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 += OnClientConnected;
    10.             _networkManager.OnClientDisconnectCallback += OnClientDisconnect;
    11.         }
    For your application quit issue, they might be some errors that preventing your application shut down successfully. For the logs, can you check your build configuration? Maybe your log file name is changed. Also if you try to see it on website for unity gaming services, you can't see it at first click. It will download the log file but you will see an empty logs on website. Then the second show attempt will show you the logs. Also you can check it on downloads. I don't think that it was the problem on the logs but maybe it can help.

    Also can you share your events tab on server? Do you see any exit signal on there? With code 0 or something else
     
    Last edited: Jan 4, 2024