onClose

说明:

  1. callback Connection::$onClose

此回调与Worker::$onClose回调作用相同,区别是只针对当前连接有效,也就是可以针对某个连接的设置onClose回调。

范例

  1. use Workerman\Worker;
  2. require_once __DIR__ . '/Workerman/Autoloader.php';
  3. $worker = new Worker('websocket://0.0.0.0:8484');
  4. // 当有链接事件时触发
  5. $worker->onConnect = function($connection)
  6. {
  7. // 设置连接的onClose回调
  8. $connection->onClose = function($connection)
  9. {
  10. echo "connection closed\n";
  11. };
  12. };
  13. // 运行worker
  14. Worker::runAll();

上面代码与下面的效果相同

  1. use Workerman\Worker;
  2. require_once __DIR__ . '/Workerman/Autoloader.php';
  3. $worker = new Worker('websocket://0.0.0.0:8484');
  4. // 设置所有连接的onclose回调
  5. $worker->onClose = function($connection)
  6. {
  7. echo "connection closed\n";
  8. };
  9. // 运行worker
  10. Worker::runAll();