/// <summary> /// Invokes a callback when connection is established, or after the timeout /// (even if failed to connect). If already connected, callback is invoked instantly /// </summary> /// <param name="connectionCallback"></param> /// <param name="timeoutSeconds"></param> public void WaitConnection(Action <IClientSocket> connectionCallback, float timeoutSeconds) { if (IsConnected) { connectionCallback.Invoke(this); return; } var isConnected = false; var timedOut = false; Action onConnected = null; onConnected = () => { Connected -= onConnected; isConnected = true; if (!timedOut) { connectionCallback.Invoke(this); } }; Connected += onConnected; LTimer.AfterSeconds(timeoutSeconds, () => { if (!isConnected) { timedOut = true; Connected -= onConnected; connectionCallback.Invoke(this); } }); }