Using php to write sokket to connect to a third-party server, we need to keep the connection going and send a heartbeat to the other side every three minutes. php is a single thread. how can this be achieved?

Source: Internet
Author: User
The business process is like this. our php Server uses socket to connect to the other server, and the other server will send messages to our php server from time to time. Therefore, we need to read the php server all the time, at the same time, you also need to send a heartbeat to each other every three minutes. When php keeps reading, the other party does not send a message... the business process is like this. our php Server uses socket to connect to the other server, and the other server will send messages to our php server from time to time. Therefore, we need to read the php server all the time, at the same time, you also need to send a heartbeat to the other party every three minutes. When php keeps reading and the other party does not receive any message, php is always waiting, how can I ensure a heartbeat every three minutes?

Reply content:

The business process is like this. our php Server uses socket to connect to the other server, and the other server will send messages to our php server from time to time. Therefore, we need to read the php server all the time, at the same time, you also need to send a heartbeat to the other party every three minutes. When php keeps reading and the other party does not receive any message, php is always waiting, how can I ensure a heartbeat every three minutes?

Generally, php is not recommended.

3-minute sleep delay (3)
Always execute the script set_time_limit (0)
Disconnect from the client and continue executing ignore_user_abort (0)

You can consider using swoole

You also need to send heartbeat to it when you read it. use php-cli to do it.

Swoole can easily meet your needs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.