Home  >  Article  >  PHP Framework  >  How to keep swoole from being offline

How to keep swoole from being offline

尚
Original
2019-12-06 14:38:212655browse

How to keep swoole from being offline

Under normal circumstances, when the client interrupts the TCP connection, it will send a FIN packet and perform 4 disconnection handshakes to notify the server. However, in some abnormal situations, such as a sudden power outage or network abnormality on the client side, the server may not be able to know that the client has disconnected.

Especially for mobile networks, TCP connections are very unstable, so a mechanism is needed to ensure the validity of the connection between the server and the client.

The Swoole extension itself has this mechanism built in, and developers only need to configure one parameter to enable it. Swoole will record a timestamp every time it receives client data. When the client does not send data to the server within a certain period of time, the server will automatically cut off the connection. (Recommended learning: swoole video tutorial)

Usage:

$serv->set(array(
    'heartbeat_check_interval' => 5,
    'heartbeat_idle_time' => 10,
));

The above setting is to detect a heartbeat every 5 seconds. If a TCP connection is connected within 10 seconds If no data is sent to the server, it will be cut off.

Advanced usage:

Use the swoole_server::heartbeat() function to manually detect whether the heartbeat has expired. This function returns all TCP connections that have been idle longer than heartbeat_idle_time. The program can perform some operations on these connections, such as sending data or closing the connection.

The above is the detailed content of How to keep swoole from being offline. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn