Home > PHP Framework > Swoole > body text

How to keep swoole from being offline

Release: 2019-12-06 14:38:21
Original
2642 people have browsed it

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,
));
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!