Search Unity

Bug: NetworkManager's OnServerDisconnect function is never called for Host

Discussion in 'Multiplayer' started by mischa2k, Jan 15, 2016.

  1. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    I attached a simple project with 13 lines of code that shows how NetworkManager:OnServerDisconnect is never called when the host disconnects. This is a problem because often game servers need to save player data after the player quits the game. This works fine for clients that connect to the server, because OnServerDisconnect is called when those clients quit the game.

    It is however not called when I run the server as host (aka I also play on it) and then stop the server (either with the Unity Editor stop button, or with the GUI stop button in the NetworkManagerHud). I think it should be called because we need some kind of "onquit" function to save the host data too. OnServerDisconnect would be perfect for this because it happens before "OnDestroy", hence we can be 100% sure that the player objects hasn't been destroyed yet and can still be saved.

    How to reproduce:
    • Open the Project, open the Scene
    • Press Play
    • Select "Start Host"
    • Stop it
    • See how there is no log message because NetworkManagerCustom:OnServerDisconnect is not called
    [Tested on Unity 5.3.1f1]
     
    Last edited: Jan 15, 2016
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    StopHost does not trigger OnServerDisconnect.. it shuts down the server and client, there is no disconnect event.
     
  3. Justice0Juic3

    Justice0Juic3

    Joined:
    Apr 29, 2013
    Posts:
    188
    Does that mean I have to code seperately what happens once the Client or Host disconnects?
     
  4. AM-Dev

    AM-Dev

    Joined:
    Aug 5, 2015
    Posts:
    31
    Hi there,
    how did you solve the issue for Host-Quit?
     
  5. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    I have to save every few seconds and hope for the best. I still haven't found any better way.
     
  6. Deleted User

    Deleted User

    Guest

    If you are Server, it will not call ServerDisconnect. But if Client will disconnect it Will call on Server.