Write Sokket with PHP to connect a third-party server, to keep the connection, every three minutes to each other to send a heartbeat, PHP is single-threaded, how can this be achieved?

Source: Internet
Author: User
Tags php server
Business process is like this, we PHP server with the socket connection to the other server, the other server will send a message to our PHP server, so we need to read,read PHP always need to send a heartbeat every three minutes to each other, when PHP has been read, When the other party did not send the message, PHP has been waiting, how to ensure that every three minutes to send a heartbeat?

Reply content:

Business process is like this, we PHP server with the socket connection to the other server, the other server will send a message to our PHP server, so we need to read,read PHP always need to send a heartbeat every three minutes to each other, when PHP has been read, When the other party did not send the message, PHP has been waiting, how to ensure that every three minutes to send a heartbeat?

Generally this does not use PHP to do very well.

Delay 3 minutes sleep (3)
Always execute script set_time_limit (0)
Disconnect from client continue execution Ignore_user_abort (0)

You can consider using Swoole

When you read, you have to give it a heartbeat, do it with PHP-CLI.

Swoole can easily take care of your needs

  • Related Article

    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.