How to Stop Allocated server?

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.

9278971--1299838--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

    public NetworkServer(NetworkManager networkManager)
    {
        _networkManager = networkManager;
        _networkManager.OnServerStarted += OnNetworkReady;
    }

    private void OnNetworkReady()
    {
        _networkManager.OnClientConnectedCallback += OnClientDisconnect;
    }

    private void OnClientDisconnect(ulong clientID)
    {
        if (!_networkManager) return;

        if (_networkManager.ConnectedClients.Count == 0)
        {
            Dispose();
        }
    }

    public void Dispose()
    {
         if(!_networkManager) return;

        _networkManager.OnClientDisconnectCallback -= OnClientDisconnect;
        _networkManager.OnServerStarted -= OnNetworkReady;

         if (_networkManager.IsListening)
         {
               _networkManager.Shutdown();
         }
     }

i got this but it seems not working.

1 Like

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.

    #region Components
    private NetworkManager _networkManager;
    #endregion

    #region Variables

    private List<ulong> _connectedClients = new();

    #endregion


    public NetworkServer(NetworkManager networkManager)
    {
        _networkManager = networkManager;
        _networkManager.OnServerStarted += OnNetworkReady;
    }

    private void OnNetworkReady()
    {
        _networkManager.OnClientConnectedCallback += OnClientConnected;
        _networkManager.OnClientDisconnectCallback += OnClientDisconnect;
    }

    private void OnClientConnected(ulong clientID)
    {
        _connectedClients.Add(clientID);
        NetworkLog.LogInfoServer($"{clientID} connected. Current client count : {_connectedClients.Count}");
    }

    private void OnClientDisconnect(ulong clientID)
    {
        if (!_networkManager || !_connectedClients.Contains(clientID)) return;

        _connectedClients.Remove(clientID);
        NetworkLog.LogInfoServer($"{clientID} disconnected. Current client count : {_connectedClients.Count}");

        if (_connectedClients.Count == 0)
        {
            Application.Quit(0);
        }
    }

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

9281266--1300384--upload_2023-9-8_14-55-18.png

1 Like

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.

1 Like

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...


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?


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.

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

    public NetworkServer(NetworkManager networkManager)
        {
            _networkManager = networkManager;
            _networkManager.OnServerStarted += OnNetworkReady;
        }

        private void OnNetworkReady()
        {
            _networkManager.OnClientConnectedCallback += OnClientConnected;
            _networkManager.OnClientDisconnectCallback += OnClientDisconnect;
        }

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