Skip to content

Commit

Permalink
fix: making sure server/client stops if gameobject is destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Dec 29, 2021
1 parent 4863071 commit 9cdc27d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Assets/Mirage/Runtime/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ internal void OnAuthenticated(INetworkPlayer player)
_authenticated.Invoke(player);
}

private void OnDestroy()
{
if (Active)
Disconnect();
}

/// <summary>
/// Disconnect from server.
/// <para>The disconnect message will be invoked.</para>
Expand Down
8 changes: 7 additions & 1 deletion Assets/Mirage/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ public class NetworkServer : MonoBehaviour, INetworkServer
public SyncVarSender SyncVarSender { get; private set; }
public MessageHandler MessageHandler { get; private set; }

private void OnDestroy()
{
// if gameobject with server on is destroyed, stop the server
if (Active)
Stop();
}

/// <summary>
/// This shuts down the server and disconnects all clients.
Expand Down Expand Up @@ -465,7 +471,7 @@ void OnDisconnected(INetworkPlayer player)
if (logger.LogEnabled()) logger.Log("Server disconnect client:" + player);

// set the flag first so we dont try to send any messages to the disconnected
// connection as they wouldn't get them
// connection as they wouldn't get them
player.MarkAsDisconnected();

RemoveConnection(player);
Expand Down

0 comments on commit 9cdc27d

Please sign in to comment.