@@ -71,6 +71,9 @@ class ReactMqttClient extends EventEmitter
71
71
private $ isConnecting = false ;
72
72
/** @var bool */
73
73
private $ isDisconnecting = false ;
74
+ /** @var callable */
75
+ private $ onCloseCallback ;
76
+
74
77
75
78
/** @var TimerInterface[] */
76
79
private $ timer = [];
@@ -227,9 +230,10 @@ public function connect($host, $port = 1883, Connection $connection = null, $tim
227
230
/**
228
231
* Disconnects from a broker.
229
232
*
233
+ * @param int $timeout
230
234
* @return ExtendedPromiseInterface
231
235
*/
232
- public function disconnect ()
236
+ public function disconnect ($ timeout = 5 )
233
237
{
234
238
if (!$ this ->isConnected || $ this ->isDisconnecting ) {
235
239
return new RejectedPromise (new \LogicException ('The client is not connected. ' ));
@@ -239,21 +243,36 @@ public function disconnect()
239
243
240
244
$ deferred = new Deferred ();
241
245
242
- $ this ->startFlow (new OutgoingDisconnectFlow ($ this ->connection ), true )
243
- ->then (function (Connection $ connection ) use ($ deferred ) {
244
- $ this ->isDisconnecting = false ;
245
- $ this ->isConnected = false ;
246
+ $ isResolved = false ;
246
247
247
- $ this ->emit ('disconnect ' , [$ connection , $ this ]);
248
- $ deferred ->resolve ($ connection );
248
+ $ this ->onCloseCallback = function ($ connection ) use ($ deferred , &$ isResolved ) {
249
+ if (!$ isResolved ) {
250
+ $ isResolved = true ;
249
251
250
- if ($ this ->stream !== null ) {
251
- $ this ->stream ->close ();
252
+ if ($ connection ) {
253
+ $ this ->emit ('disconnect ' , [$ connection , $ this ]);
254
+ $ deferred ->resolve ($ connection );
252
255
}
256
+ }
257
+ };
258
+
259
+ $ this ->startFlow (new OutgoingDisconnectFlow ($ this ->connection ), true )
260
+ ->then (function () use ($ timeout ) {
261
+ $ this ->timer [] = $ this ->loop ->addTimer (
262
+ $ timeout ,
263
+ function () {
264
+ if ($ this ->stream !== null ) {
265
+ $ this ->stream ->close ();
266
+ }
267
+ }
268
+ );
253
269
})
254
- ->otherwise (function () use ($ deferred ) {
255
- $ this ->isDisconnecting = false ;
256
- $ deferred ->reject ($ this ->connection );
270
+ ->otherwise (function () use ($ deferred , &$ isResolved ) {
271
+ if (!$ isResolved ) {
272
+ $ isResolved = true ;
273
+ $ this ->isDisconnecting = false ;
274
+ $ deferred ->reject ($ this ->connection );
275
+ }
257
276
});
258
277
259
278
return $ deferred ->promise ();
@@ -569,6 +588,11 @@ private function handleClose()
569
588
$ this ->connection = null ;
570
589
$ this ->stream = null ;
571
590
591
+ if ($ this ->onCloseCallback !== null ) {
592
+ call_user_func ($ this ->onCloseCallback , $ connection );
593
+ $ this ->onCloseCallback = null ;
594
+ }
595
+
572
596
if ($ connection !== null ) {
573
597
$ this ->emit ('close ' , [$ connection , $ this ]);
574
598
}
0 commit comments