Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NetworkManager):Allow host client to start late or rejoin #3661

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions Assets/Mirror/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,17 @@ public void StartClient()
return;
}

mode = NetworkManagerMode.ClientOnly;
if (mode == NetworkManagerMode.ServerOnly)
{
mode = NetworkManagerMode.Host;

// Redirect to FinishStartHost to do all the proper setup for host mode
// Exit here after that's completed, instead of proceeding as a remote client.
FinishStartHost();
return;
}
else
mode = NetworkManagerMode.ClientOnly;

SetupClient();

Expand Down Expand Up @@ -1309,8 +1319,18 @@ void OnClientDisconnectInternal()
// shutdown client
NetworkClient.Shutdown();

// Exit here if we're now in ServerOnly mode (StopClient called in Host mode).
if (mode == NetworkManagerMode.ServerOnly) return;
// If we're in ServerOnly mode now, StopClient was called for host client.
// We need to reset clientStarted to false so that when we return to
// Host mode, we properly respawn objects from StartClient again.
if (mode == NetworkManagerMode.ServerOnly)
{
foreach (NetworkIdentity identity in NetworkServer.spawned.Values)
identity.clientStarted = false;

// Exit here if we're now in ServerOnly mode (StopClient called in Host mode).
// This allows the server to keep running without the host client, keeping remote clients connected.
return;
}

// Get Network Manager out of DDOL before going to offline scene
// to avoid collision and let a fresh Network Manager be created.
Expand Down
8 changes: 8 additions & 0 deletions Assets/Mirror/Core/NetworkManagerHUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,17 @@ void StopButtons()
}
else if (NetworkServer.active)
{
GUILayout.BeginHorizontal();

// stop server if server-only
if (GUILayout.Button("Stop Server"))
manager.StopServer();

// start client if server-only, engaging host mode.
if (GUILayout.Button("Start Client"))
manager.StartClient();

GUILayout.EndHorizontal();
}
}
}
Expand Down