public virtual IObservable<bool> ConnectAsync(string serverAddress, string applicationName)
{
ValidateDisposed();
var future = new AsyncSubject<bool>();
var watchStatusChanged = new SingleAssignmentDisposable();
watchStatusChanged.Disposable = this.ObserveStatusChanged().Subscribe(x =>
{
if (x == StatusCode.ExceptionOnConnect)
{
watchStatusChanged.Dispose();
future.OnError(new ConnectionFailedException(serverAddress, applicationName));
}
if (x == StatusCode.Connect)
{
watchStatusChanged.Dispose();
future.OnNext(true);
future.OnCompleted();
}
});
if (this.Connect(serverAddress, applicationName))
{
this.LastConnectServerAddress = serverAddress;
this.LastConnectApplicationName = applicationName;
return future.Timeout(Timeout).Catch((Exception ex) =>
{
watchStatusChanged.Dispose();
this.Disconnect();
return Observable.Throw<bool>(ex);
});
}
else
{
watchStatusChanged.Dispose();
return Observable.Return(false);
}
}