Forráskód Böngészése

Implemented WebSocket connected callback.

Ako Tulu 1 éve
szülő
commit
515217652a
3 módosított fájl, 30 hozzáadás és 4 törlés
  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

@@ -110,13 +110,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

@@ -382,12 +382,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);
                 }
@@ -413,10 +415,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