Procházet zdrojové kódy

Merge branch 'master' of github.com:walkor/workerman

walkor před 1 rokem
rodič
revize
d12398739e

+ 8 - 1
src/Connection/TcpConnection.php

@@ -124,13 +124,20 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
     public $onConnect = null;
 
     /**
-     * Emitted when websocket handshake completed (Only work when protocol is ws).
+     * Emitted before websocket handshake (Only works when protocol is ws).
      *
      * @var ?callable
      */
     public $onWebSocketConnect = null;
 
     /**
+     * Emitted after websocket handshake (Only works when protocol is ws).
+     *
+     * @var ?callable
+     */
+    public $onWebSocketConnected = null;
+
+    /**
      * Emitted when data is received.
      *
      * @var ?callable

+ 5 - 0
src/Events/Swoole.php

@@ -15,6 +15,7 @@ declare(strict_types=1);
 
 namespace Workerman\Events;
 
+use Swoole\Coroutine;
 use Swoole\Event;
 use Swoole\Process;
 use Swoole\Timer;
@@ -211,6 +212,10 @@ final class Swoole implements EventInterface
      */
     public function stop(): void
     {
+        // cancel all coroutines before Event::exit
+        foreach (Coroutine::listCoroutines() as $coroutine) {
+            Coroutine::cancel($coroutine);
+        }
         Event::exit();
         posix_kill(posix_getpid(), SIGINT);
     }

+ 14 - 2
src/Protocols/Websocket.php

@@ -458,12 +458,14 @@ class Websocket
             $connection->context->websocketCurrentFrameBuffer = '';
             // Consume handshake data.
             $connection->consumeRecvBuffer($headerLength);
+            // Request from buffer
+            $request = new Request($buffer);
 
             // Try to emit onWebSocketConnect callback.
             $onWebsocketConnect = $connection->onWebSocketConnect ?? $connection->worker->onWebSocketConnect ?? false;
             if ($onWebsocketConnect) {
                 try {
-                    $onWebsocketConnect($connection, new Request($buffer));
+                    $onWebsocketConnect($connection, $request);
                 } catch (Throwable $e) {
                     Worker::stopAll(250, $e);
                 }
@@ -489,10 +491,20 @@ class Websocket
             }
             $handshakeMessage .= "\r\n";
             // Send handshake response.
-            $connection->send($handshakeMessage, true);
+            $status = $connection->send($handshakeMessage, true);
             // Mark handshake complete.
             $connection->context->websocketHandshake = true;
 
+            // Try to emit onWebSocketConnected callback.
+            $onWebsocketConnected = $connection->onWebSocketConnected ?? $connection->worker->onWebSocketConnected ?? false;
+            if ($status && $onWebsocketConnected) {
+                try {
+                    $onWebsocketConnected($connection, $request);
+                } catch (Throwable $e) {
+                    Worker::stopAll(250, $e);
+                }
+            }
+
             // There are data waiting to be sent.
             if (!empty($connection->context->tmpWebsocketData)) {
                 $connection->send($connection->context->tmpWebsocketData, true);

+ 9 - 2
src/Worker.php

@@ -51,7 +51,7 @@ class Worker
      *
      * @var string
      */
-    final public const VERSION = '5.0.0-beta.7';
+    final public const VERSION = '5.0.0-rc.1';
 
     /**
      * Status starting.
@@ -159,13 +159,20 @@ class Worker
     public $onConnect = null;
 
     /**
-     * Emitted when websocket handshake did complete (Only called when protocol is ws).
+     * Emitted before websocket handshake (Only works when protocol is ws).
      *
      * @var ?callable
      */
     public $onWebSocketConnect = null;
 
     /**
+     * Emitted after websocket handshake (Only works when protocol is ws).
+     *
+     * @var ?callable
+     */
+    public $onWebSocketConnected = null;
+
+    /**
      * Emitted when data is received.
      *
      * @var ?callable