callback Worker::$onClose

The callback function triggered when a client's connection is disconnected from Workerman. Regardless of how the connection is terminated, the onClose event will be triggered. Each connection will trigger onClose only once.

Note: If the connection is terminated due to extreme circumstances such as loss of network or power, Workerman may not be able to timely send a TCP fin packet to know that the connection has been terminated, and therefore cannot trigger onClose promptly. This kind of situation needs to be resolved by using application-layer heartbeats. For the implementation of connection heartbeats in Workerman, refer to here. If using the GatewayWorker framework, simply utilize the heartbeat mechanism provided by the GatewayWorker framework, refer to here.

Since UDP is connectionless, when using UDP, the onConnect callback will not be triggered, and the onClose callback will not be triggered either.

Callback Function Parameters


Connection object, i.e. TcpConnection instance, used to operate client connections such as sending data, closing connections, etc.


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

$worker = new Worker('websocket://');
$worker->onClose = function(TcpConnection $connection)
    echo "connection closed\n";
// Run worker

Note: In addition to using an anonymous function as a callback, you can also refer to here for other callback writing methods.