From a056072aecb4336e155e85d739fc4a3e95ad4033 Mon Sep 17 00:00:00 2001 From: vis2k Date: Tue, 11 May 2021 11:36:05 +0800 Subject: [PATCH] NetworkClient.Disconnect: cleanup code moved into OnTransportDisconnected --- Assets/Mirror/Runtime/NetworkClient.cs | 61 ++++++++++++++------------ 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 50bb2aaa2..27b3f4e3d 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -205,36 +205,11 @@ public static void ConnectLocalServer() // disconnect ////////////////////////////////////////////////////////// /// Disconnect from server. + // Simply call NetworkConnection.Disconnect -> Transport.Disconnect. + // Cleanup happens in OnTransportDisconnected! public static void Disconnect() { - connectState = ConnectState.Disconnected; - ready = false; - - // local or remote connection? - if (isLocalClient) - { - if (isConnected) - { - // call client OnDisconnected with connection to server - // => previously we used to send a DisconnectMessage to - // NetworkServer.localConnection. this would queue the - // message until NetworkClient.Update processes it. - // => invoking the client's OnDisconnected event directly - // here makes tests fail. so let's do it exactly the same - // order as before by queueing the event for next Update! - //OnDisconnectedEvent?.Invoke(connection); - ((LocalConnectionToServer)connection).QueueDisconnectedEvent(); - } - NetworkServer.RemoveLocalConnection(); - } - else - { - if (connection != null) - { - connection.Disconnect(); - connection = null; - } - } + connection?.Disconnect(); } /// Disconnect host mode. @@ -291,6 +266,36 @@ static void OnTransportDisconnected() connectState = ConnectState.Disconnected; ready = false; + // this was in Disconnect() before. + // but it should happen after truly disconnecting here. + + // local or remote connection? + if (isLocalClient) + { + // TODO move into LocalConnection.Disconnect + if (isConnected) + { + // call client OnDisconnected with connection to server + // => previously we used to send a DisconnectMessage to + // NetworkServer.localConnection. this would queue the + // message until NetworkClient.Update processes it. + // => invoking the client's OnDisconnected event directly + // here makes tests fail. so let's do it exactly the same + // order as before by queueing the event for next Update! + //OnDisconnectedEvent?.Invoke(connection); + ((LocalConnectionToServer)connection).QueueDisconnectedEvent(); + } + NetworkServer.RemoveLocalConnection(); + } + else + { + if (connection != null) + { + connection.Disconnect(); + connection = null; + } + } + if (connection != null) OnDisconnectedEvent?.Invoke(); }