Skip to content

Commit

Permalink
fix: properly stop client and server in OnApplicationQuit so that cli…
Browse files Browse the repository at this point in the history
…ents still get a chance to send then 'quit' packet instead of just timing out. Also fixes a bug where OnStopServer/OnStopClient were not called when stopping the Editor. (#936)
  • Loading branch information
miwarnec committed Jun 28, 2019
1 parent 7181cd9 commit d6389e6
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions Assets/Mirror/Runtime/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,33 @@ public virtual void LateUpdate()
UpdateScene();
}

// When pressing Stop in the Editor, Unity keeps threads alive until we
// press Start again (which might be a Unity bug).
// Either way, we should disconnect client & server in OnApplicationQuit
// so they don't keep running until we press Play again.
// (this is not a problem in builds)
// called when quitting the application by closing the window / pressing
// stop in the editor
//
// virtual so that inheriting classes' OnApplicationQuit() can call base.OnApplicationQuit() too
// virtual so that inheriting classes' OnApplicationQuit() can call
// base.OnApplicationQuit() too
public virtual void OnApplicationQuit()
{
// stop client first
// (we want to send the quit packet to the server instead of waiting
// for a timeout)
if (NetworkClient.isConnected)
{
StopClient();
print("OnApplicationQuit: stopped client");
}

// stop server after stopping client (for proper host mode stopping)
if (NetworkServer.active)
{
StopServer();
print("OnApplicationQuit: stopped server");
}

// stop transport (e.g. to shut down threads)
// (when pressing Stop in the Editor, Unity keeps threads alive
// until we press Start again. so if Transports use threads, we
// really want them to end now and not after next start)
Transport.activeTransport.Shutdown();
}

Expand Down

0 comments on commit d6389e6

Please sign in to comment.