@@ -351,8 +351,8 @@ public function unsubscribe(): bool {
351351 /**
352352 * Idle the current connection
353353 * @param callable $callback
354- * @param integer $timeout max 1740 seconds - recommended by rfc2177 §3
355- * @param boolean $auto_reconnect try to reconnect on connection close
354+ * @param integer $timeout max 1740 seconds - recommended by rfc2177 §3. Should not be lower than the servers "* OK Still here" message interval
355+ * @param boolean $auto_reconnect try to reconnect on connection close (@deprecated is no longer required)
356356 *
357357 * @throws ConnectionFailedException
358358 * @throws Exceptions\InvalidMessageDateException
@@ -364,25 +364,24 @@ public function unsubscribe(): bool {
364364 * @throws Exceptions\MessageNotFoundException
365365 * @throws Exceptions\NotSupportedCapabilityException
366366 */
367- public function idle (callable $ callback , int $ timeout = 1200 , bool $ auto_reconnect = false ) {
368- $ this ->client ->getConnection ()->setConnectionTimeout ($ timeout );
369-
370- $ this ->client ->reconnect ();
367+ public function idle (callable $ callback , int $ timeout = 300 , bool $ auto_reconnect = false ) {
368+ $ this ->client ->setTimeout ($ timeout );
371369 if (!in_array ("IDLE " , $ this ->client ->getConnection ()->getCapabilities ())) {
372370 throw new NotSupportedCapabilityException ("IMAP server does not support IDLE " );
373371 }
374372 $ this ->client ->openFolder ($ this ->path , true );
375373 $ connection = $ this ->client ->getConnection ();
374+ $ connection ->idle ();
376375
377376 $ sequence = ClientManager::get ('options.sequence ' , IMAP ::ST_MSGN );
378- $ connection ->idle ();
379377
380378 while (true ) {
381379 try {
382380 $ line = $ connection ->nextLine ();
381+
383382 if (($ pos = strpos ($ line , "EXISTS " )) !== false ) {
384- $ msgn = (int ) substr ($ line , 2 , $ pos -2 );
385383 $ connection ->done ();
384+ $ msgn = (int ) substr ($ line , 2 , $ pos -2 );
386385
387386 $ this ->client ->openFolder ($ this ->path , true );
388387 $ message = $ this ->query ()->getMessageByMsgn ($ msgn );
@@ -391,20 +390,26 @@ public function idle(callable $callback, int $timeout = 1200, bool $auto_reconne
391390
392391 $ event = $ this ->getEvent ("message " , "new " );
393392 $ event ::dispatch ($ message );
394-
393+ $ connection ->idle ();
394+ } elseif (strpos ($ line , "OK " ) === false ) {
395+ $ connection ->done ();
395396 $ connection ->idle ();
396397 }
397398 }catch (Exceptions \RuntimeException $ e ) {
399+ if (strpos ($ e ->getMessage (), "empty response " ) >= 0 && $ connection ->connected ()) {
400+ $ connection ->done ();
401+ $ connection ->idle ();
402+ continue ;
403+ }
398404 if (strpos ($ e ->getMessage (), "connection closed " ) === false ) {
399405 throw $ e ;
400406 }
401- if ($ auto_reconnect === true ) {
402- $ this ->client ->reconnect ();
403- $ this ->client ->openFolder ($ this ->path , true );
404407
405- $ connection = $ this ->client ->getConnection ();
406- $ connection ->idle ();
407- }
408+ $ this ->client ->reconnect ();
409+ $ this ->client ->openFolder ($ this ->path , true );
410+
411+ $ connection = $ this ->client ->getConnection ();
412+ $ connection ->idle ();
408413 }
409414 }
410415 }
0 commit comments