Ver Fonte

Merge pull request #1055 from akotulu/master

Implemented WebSocket connected callback.
walkor há 1 ano atrás
pai
commit
219e2a1a56
3 ficheiros alterados com 30 adições e 4 exclusões
  1. 8 1
      src/Connection/TcpConnection.php
  2. 14 2
      src/Protocols/Websocket.php
  3. 8 1
      src/Worker.php

+ 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

+ 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);

+ 8 - 1
src/Worker.php

@@ -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