In previous versions, the multi-port listening heartbeat detection function can only be configured on the main service, and the heartbeat time cannot be set for each port individually.

For example, you need to set 30 seconds on port 9501 and 60 seconds on port 9502. Support has been available since v4.7. Let’s take a closer look.

Configuration items

Two configuration items are provided in the Server: heartbeat_check_interval and heartbeat_IDLE_time.

You can use the following configuration items to add heartbeat detection:

$server->set([
    'heartbeat_check_interval'= >60.'heartbeat_idle_time'= >120,]);Copy the code

Heartbeat_check_interval Indicates how often the cycle is performed, in seconds. For example, if heartbeat_check_interval => 60, all connections are traversed every 60 seconds.

If the connection does not send any data to the server within 120 seconds (heartbeat_IDLE_TIME defaults to twice interval if it is not set), the connection will be forcibly closed.

Heartbeat_idle_time Indicates the maximum idle time of a connection.

The sample

Here provides a multi-port listening code for testing, respectively set up heartbeat detection for different ports:

To facilitate the test, set the heartbeat detection time to 1 second

use Swoole\Server;

$server = new Server('127.0.0.1'.9501, SWOOLE_BASE);
$server->set([
    'heartbeat_check_interval'= >1.'heartbeat_idle_time'= >1,]);$server->on('connect'.function ($server.$fd) {
    $time = date('Y-m-d H:i:s');
    echo "[{$time}] Client#{$fd}: Connect.\n";
});
$server->on('receive'.function ($server.$fd.$reactorId.$data) {
    $server->send($fd.'ok');
});
$server->on('close'.function ($server.$fd) {
    $time = date('Y-m-d H:i:s');
    echo "[{$time}] Client#{$fd}: Close.\n";
});

$port2 = $server->listen('127.0.0.1'.9502, SWOOLE_SOCK_TCP);
$port2->set([
    'heartbeat_idle_time'= >2,]);$port3 = $server->listen('127.0.0.1'.9503, SWOOLE_SOCK_TCP);
$port3->set([
    'heartbeat_idle_time'= >10,]);$server->start();
Copy the code

You can use Telnet or Swoole TCP clients for testing.

Telnet is used here to test, connecting 3 ports separately

Telnet 127.0.0.1 9501 Telnet 127.0.0.1 9502 Telnet 127.0.0.1 9503Copy the code

Testing with v4.6 produces:

[2021-07-05 10:06:44] Client#1: Connect.
[2021-07-05 10:06:45] Client#2: Connect.
[2021-07-05 10:06:46] Client#3: Connect.
[2021-07-05 10:06:46] Client#1: Close.
[2021-07-05 10:06:47] Client#2: Close.
[2021-07-05 10:06:48] Client#3: Close.
Copy the code

Connections 1, 2, and 3 were all disconnected after 2 seconds.

Then use the latest v4.7 version to test:

[2021-07-05 10:02:50] Client#1: Connect.
[2021-07-05 10:02:51] Client#2: Connect.
[2021-07-05 10:02:51] Client#1: Close.
[2021-07-05 10:02:52] Client#3: Connect.
[2021-07-05 10:02:53] Client#2: Close.
[2021-07-05 10:03:02] Client#3: Close.
Copy the code
  • The connection11Disconnect after seconds;
  • The connection22Disconnect after seconds;
  • The connection310Disconnect after seconds.

The output is consistent with the configured heartbeat detection configuration. Users who need to use the heartbeat detection function can upgrade experience.