Skip to content

Commit

Permalink
Merge pull request #1055 from akotulu/master
Browse files Browse the repository at this point in the history
Implemented WebSocket connected callback.
  • Loading branch information
walkor authored Sep 17, 2024
2 parents ca0199e + 5152176 commit 219e2a1
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 @@ -124,12 +124,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 @@ -458,12 +458,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 @@ -489,10 +491,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 219e2a1

Please sign in to comment.