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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
