walkor hace 2 años
padre
commit
cd95963616
Se han modificado 3 ficheros con 25 adiciones y 8 borrados
  1. 1 0
      src/Connection/TcpConnection.php
  2. 16 7
      src/Timer.php
  3. 8 1
      src/Worker.php

+ 1 - 0
src/Connection/TcpConnection.php

@@ -21,6 +21,7 @@ use Workerman\Worker;
 
 /**
  * TcpConnection.
+ * @property string websocketType
  */
 class TcpConnection extends ConnectionInterface implements \JsonSerializable
 {

+ 16 - 7
src/Timer.php

@@ -13,6 +13,7 @@
  */
 namespace Workerman;
 
+use Revolt\EventLoop;
 use Workerman\Events\EventInterface;
 use Workerman\Events\Select;
 use Workerman\Worker;
@@ -97,10 +98,10 @@ class Timer
      * @param float    $timeInterval
      * @param callable $func
      * @param mixed    $args
-     * @param bool     $persistent
+     * @param bool $persistent
      * @return int|bool
      */
-    public static function add(float $timeInterval, $func, $args = [], $persistent = true)
+    public static function add(float $timeInterval, callable $func, $args = [], bool $persistent = true)
     {
         if ($timeInterval < 0) {
             Worker::safeEcho(new Exception("bad time_interval"));
@@ -117,7 +118,7 @@ class Timer
         
         // If not workerman runtime just return.
         if (!Worker::getAllWorkers()) {
-            return;
+            return false;
         }
 
         if (!\is_callable($func)) {
@@ -145,14 +146,22 @@ class Timer
      * @param float $delay
      * @param $func
      * @param array $args
-     * @return bool|int
+     * @return bool|int|null
      */
-    public static function delay(float $delay, $func, $args = [])
+    public static function delay(float $delay, $func = null, array $args = [])
     {
+        if (!$func) {
+            $eventLoop = EventLoop::getDriver();
+            $suspension = $eventLoop->getSuspension();
+            static::add($delay, function () use ($suspension) {
+                $suspension->resume();
+            }, $args, false);
+            $suspension->suspend();
+            return null;
+        }
         return static::add($delay, $func, $args, false);
     }
 
-
     /**
      * Tick.
      *
@@ -170,7 +179,7 @@ class Timer
                 foreach ($taskData as $index => $oneTask) {
                     $taskFunc     = $oneTask[0];
                     $taskArgs     = $oneTask[1];
-                    $persistent    = $oneTask[2];
+                    $persistent   = $oneTask[2];
                     $timeInterval = $oneTask[3];
                     try {
                         $taskFunc(...$taskArgs);

+ 8 - 1
src/Worker.php

@@ -19,7 +19,9 @@ use Workerman\Connection\ConnectionInterface;
 use Workerman\Connection\TcpConnection;
 use Workerman\Connection\UdpConnection;
 use Workerman\Events\Event;
+use Workerman\Events\Revolt;
 use Workerman\Events\Select;
+use Revolt\EventLoop;
 
 
 /**
@@ -473,7 +475,7 @@ class Worker
      * @var array<string, string>
      */
     protected static $availableEventLoops = [
-        "event" => Event::class,
+        'event' => Event::class,
     ];
 
     /**
@@ -1285,6 +1287,11 @@ class Worker
             return static::$eventLoopClass;
         }
 
+        if (\class_exists(EventLoop::class)) {
+            static::$eventLoopClass = Revolt::class;
+            return static::$eventLoopClass;
+        }
+
         $loopName = '';
         foreach (static::$availableEventLoops as $name => $class) {
             if (\extension_loaded($name)) {