onClose

Description:

callback Connection::$onClose

This callback has the same effect as the Worker::$onClose callback, with the distinction that it is only effective for the current connection, allowing the onClose callback to be set specifically for a particular connection.

Example

use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker('websocket://0.0.0.0:8484');
// Triggered when there is a connection event
$worker->onConnect = function(TcpConnection $connection)
{
    // Set the onClose callback for the connection
    $connection->onClose = function(TcpConnection $connection)
    {
        echo "connection closed\n";
    };
};
// Run worker
Worker::runAll();

The code above has the same effect as the one below

use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker('websocket://0.0.0.0:8484');
// Set the onClose callback for all connections
$worker->onClose = function(TcpConnection $connection)
{
    echo "connection closed\n";
};
// Run worker
Worker::runAll();