Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question NetworkManager.Singletion.StartServer vs NetworkManager.Singletion.Starthost in Netcode

Discussion in 'Netcode for GameObjects' started by Donkrokodil, Nov 30, 2023.

  1. Donkrokodil

    Donkrokodil

    Joined:
    Aug 16, 2019
    Posts:
    8
    Hi,
    According to the Unity Docs, the server automatically shuts down when host leaves the lobby when you use client-host game format (StartHost() and StartClient()), while there is no host migration that is directly implemented into Unity Netcode now. If I understood correctly, StartServer() doesn't require a host, so host migration is not really needed, because you start only a server.
    If so, my questions are as follows: suppose that one player starts a server using StartServer(). When will that server cease to exist if no one joins this server (or if someone joins it and then everyone leaves). Will something happen to that server, if the client, who initiated the NetworkManager.Singleton.StartServer(), quits the application or just leaves the server after joining?
     
  2. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    244
    The server will persist until you invoke NetworkManager.Shutdown. What the conditions to shutdown are up to you.
    You could have a server-side coroutine that checks the NetworkManager.ConnectedClientsList.Count every second or so and if the count is greater than 0 you could reset a "TimeWithoutClients" property. If the "TimeWithoutClients" property exceeds you pre-determined maximum time you want a server to be idle, then you could shut it down.

    When starting a server, you will have no local client and typically starting a server (StartServer) is for a hosted/dedicated server. If that application instance is exited, then the entire session is ended and any connected clients are disconnected.

    If someone starts a host (StartHost), then they have a client (i.e. can spawn local players and such) but the same applies to that instance... if the player who started the host exits then the entire session ends and any connected clients are disconnected.
     
  3. Donkrokodil

    Donkrokodil

    Joined:
    Aug 16, 2019
    Posts:
    8
    Then, If I start the server via calling StartServer, and then I leave that application instance, like alt+f4, then server will shutdown immediately?
     
  4. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    437
    yes, if you shut down the server (I.E: by doing alt + f4 or killing its process in the task manager), the server will shutdown immediately.
     
  5. Donkrokodil

    Donkrokodil

    Joined:
    Aug 16, 2019
    Posts:
    8
    Ok, then is there a possibility to maintain the server, given that the initiator of that server closed the game, crashed, etc.? Or something similar to the host migration in case of a listen-server model?