@@ -202,15 +202,15 @@ public function connect($host, $port = 1883, Connection $connection = null, $tim
202
202
$ this ->emit ('open ' , [$ connection , $ this ]);
203
203
204
204
$ this ->registerClient ($ connection , $ timeout )
205
- ->then (function (Connection $ connection ) use ($ deferred ) {
205
+ ->then (function ($ result ) use ($ connection , $ deferred ) {
206
206
$ this ->isConnecting = false ;
207
207
$ this ->isConnected = true ;
208
208
$ this ->connection = $ connection ;
209
209
210
210
$ this ->emit ('connect ' , [$ connection , $ this ]);
211
- $ deferred ->resolve ($ this -> connection );
211
+ $ deferred ->resolve ($ result ?: $ connection );
212
212
})
213
- ->otherwise (function (\Exception $ e ) use ($ deferred , $ connection ) {
213
+ ->otherwise (function (\Exception $ e ) use ($ connection , $ deferred ) {
214
214
$ this ->isConnecting = false ;
215
215
216
216
$ this ->emitError ($ e );
@@ -251,20 +251,24 @@ public function disconnect($timeout = 5)
251
251
$ deferred = new Deferred ();
252
252
253
253
$ isResolved = false ;
254
+ $ flowResult = null ;
254
255
255
- $ this ->onCloseCallback = function ($ connection ) use ($ deferred , &$ isResolved ) {
256
+ $ this ->onCloseCallback = function ($ connection ) use ($ deferred , &$ isResolved, & $ flowResult ) {
256
257
if (!$ isResolved ) {
257
258
$ isResolved = true ;
258
259
259
260
if ($ connection ) {
260
261
$ this ->emit ('disconnect ' , [$ connection , $ this ]);
261
- $ deferred ->resolve ($ connection );
262
262
}
263
+
264
+ $ deferred ->resolve ($ flowResult ?: $ connection );
263
265
}
264
266
};
265
267
266
268
$ this ->startFlow ($ this ->flowFactory ->buildOutgoingDisconnectFlow ($ this ->connection ), true )
267
- ->then (function () use ($ timeout ) {
269
+ ->then (function ($ result ) use ($ timeout , &$ flowResult ) {
270
+ $ flowResult = $ result ;
271
+
268
272
$ this ->timer [] = $ this ->loop ->addTimer (
269
273
$ timeout ,
270
274
function () {
@@ -274,11 +278,11 @@ function () {
274
278
}
275
279
);
276
280
})
277
- ->otherwise (function () use ($ deferred , &$ isResolved ) {
281
+ ->otherwise (function ($ exception ) use ($ deferred , &$ isResolved ) {
278
282
if (!$ isResolved ) {
279
283
$ isResolved = true ;
280
284
$ this ->isDisconnecting = false ;
281
- $ deferred ->reject ($ this -> connection );
285
+ $ deferred ->reject ($ exception );
282
286
}
283
287
});
284
288
@@ -461,15 +465,15 @@ function () use ($deferred, $timeout) {
461
465
$ this ->startFlow ($ this ->flowFactory ->buildOutgoingConnectFlow ($ connection ), true )
462
466
->always (function () use ($ responseTimer ) {
463
467
$ this ->loop ->cancelTimer ($ responseTimer );
464
- })->then (function (Connection $ connection ) use ($ deferred ) {
468
+ })->then (function ($ result ) use ($ connection , $ deferred ) {
465
469
$ this ->timer [] = $ this ->loop ->addPeriodicTimer (
466
470
floor ($ connection ->getKeepAlive () * 0.75 ),
467
471
function () {
468
472
$ this ->startFlow ($ this ->flowFactory ->buildOutgoingPingFlow ());
469
473
}
470
474
);
471
475
472
- $ deferred ->resolve ($ connection );
476
+ $ deferred ->resolve ($ result ?: $ connection );
473
477
})->otherwise (function (\Exception $ e ) use ($ deferred ) {
474
478
$ deferred ->reject ($ e );
475
479
});
0 commit comments