Skip to content

Commit

Permalink
fix: Call hooks when initializing objects OnStartServer on host (#1249)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpach authored and miwarnec committed Nov 29, 2019
1 parent 7119dd1 commit 7aa7815
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
15 changes: 10 additions & 5 deletions Assets/Mirror/Runtime/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ public static void Connect(string address)
connection.SetHandlers(handlers);
}

/// <summary>
/// connect host mode
/// </summary>
internal static void ConnectLocalServer()
internal static void SetupLocalConnection()
{
if (LogFilter.Debug) Debug.Log("Client Connect Local Server");

Expand All @@ -112,7 +109,15 @@ internal static void ConnectLocalServer()

// create server connection to local client
NetworkServer.SetLocalConnection(connectionToClient);
connectionToClient.Send(new ConnectMessage());

}
/// <summary>
/// connect host mode
/// </summary>
internal static void ConnectLocalServer()
{
NetworkServer.OnConnected(NetworkServer.localConnection);
NetworkServer.localConnection.Send(new ConnectMessage());
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion Assets/Mirror/Runtime/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ public void StartClient()
public virtual void StartHost()
{
OnStartHost();
NetworkClient.SetupLocalConnection();
if (StartServer())
{
ConnectLocalClient();
Expand Down Expand Up @@ -530,8 +531,8 @@ void ConnectLocalClient()

networkAddress = "localhost";
NetworkServer.ActivateLocalClientScene();
NetworkClient.ConnectLocalServer();
RegisterClientMessages();
NetworkClient.ConnectLocalServer();
}

void RegisterClientMessages()
Expand Down
12 changes: 2 additions & 10 deletions Assets/Mirror/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static class NetworkServer
/// <para>True is a local client is currently active on the server.</para>
/// <para>This will be true for "Hosts" on hosted server games.</para>
/// </summary>
public static bool localClientActive { get; private set; }
public static bool localClientActive => localConnection != null;

// cache the Send(connectionIds) list to avoid allocating each time
static readonly List<int> connectionIdsCache = new List<int>();
Expand Down Expand Up @@ -185,7 +185,6 @@ internal static void SetLocalConnection(ULocalConnectionToClient conn)
}

localConnection = conn;
OnConnected(localConnection);
}

internal static void RemoveLocalConnection()
Expand All @@ -195,17 +194,11 @@ internal static void RemoveLocalConnection()
localConnection.Disconnect();
localConnection = null;
}
localClientActive = false;
RemoveConnection(0);
}

internal static void ActivateLocalClientScene()
{
if (localClientActive)
return;

// ClientScene for a local connection is becoming active. any spawned objects need to be started as client objects
localClientActive = true;
foreach (NetworkIdentity identity in NetworkIdentity.spawned.Values)
{
if (!identity.isClient)
Expand Down Expand Up @@ -432,7 +425,6 @@ public static void DisconnectAll()
localConnection = null;

active = false;
localClientActive = false;
}

/// <summary>
Expand Down Expand Up @@ -512,7 +504,7 @@ static void OnConnected(int connectionId)
}
}

static void OnConnected(NetworkConnectionToClient conn)
internal static void OnConnected(NetworkConnectionToClient conn)
{
if (LogFilter.Debug) Debug.Log("Server accepted client:" + conn);

Expand Down

0 comments on commit 7aa7815

Please sign in to comment.