Swoole\Server->on('close', fn)

Declaration

<?php
Swoole\Server->on('close', callback $callback)

Parameters

event

The event name

callback

callback function

Return

success

if success, it returns TRUE, otherwise it returns FALSE.

Description

Executue the callback function when the Server is closing a new connection.

Callback function

<?php
function (Swoole\Server $server, int $fd, int $reactorId) {

}
  • $server the swoole server object
  • $fd the id number of client
  • $reactorId the id number of reactor thread, when the $reactorId < 0, the connection is closed by server.

Example

<?php
$server = new Swoole\Server("127.0.0.1", 9501);
$server->on('start', function ($server){
    echo "Server is started.\n";
});
$server->on('shutdown', function ($server){
    echo "Server is shuttng down.\n";
});
$server->on('connect', function ($server, $fd){
    echo "New connection established: #{$fd}.\n";
});
$server->on('receive', function ($server, $fd, $from_id, $data) {
    $server->send($fd, "Echo to #{$fd}: \n".$data);
    $server->close($fd);
});
$server->on('close', function ($server, $fd) {
    echo "Connection closed: #{$fd}.\n";
});
$server->start();