Skip to content

Commit

Permalink
Implemented WebSocket connected callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
akotulu committed Sep 2, 2024
1 parent 9e4ecd9 commit 5152176
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/Connection/TcpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,19 @@ 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.
*
Expand Down
16 changes: 14 additions & 2 deletions src/Protocols/Websocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,14 @@ public static function dealHandshake(string $buffer, TcpConnection $connection):
$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);
}
Expand All @@ -413,10 +415,20 @@ public static function dealHandshake(string $buffer, TcpConnection $connection):
}
$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);
Expand Down
9 changes: 8 additions & 1 deletion src/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,19 @@ 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.
*
Expand Down

0 comments on commit 5152176

Please sign in to comment.