PHP Customer Chat Answering system, long connection plus Ajax polling implementation

Source: Internet
Author: User

Customer Service reply system has three kinds of implementation methods:
1, with Ajax every few seconds to request a server, to see if there is no message, there is disappeared to return to display to the user.
2, long connection: After the establishment of a connection is constantly open, PHP set_time_out (0) with Ob_flushflush and other functions force push to the client
3, long connection plus polling, the establishment of a long connection, no data has been attached, when there is data, the return data, and end the current connection, the client accepts the data and display, and then connect the server side, and start a long connection, so the cycle, this is the long polling.

Today, we use the third method to implement the PHP customer service chat system.
Just write my thoughts:
-----------------------------------

Client PHP:
while (true) {
A dead loop
See if there is any data sent by customer service.
1. If not, then continue while
2, if there is that return data, and end the program
if (have) {
echo data;
Exit
}
Sleep (5);
}
-----------------------------------
Customer: Client
Window.onload = function () {
var setting = {
Type: "Post",
URL: ' kehu.php ',
DataType: ' JSON ',
Success:function () {
Show the content to the user
Hibernate for 3 seconds before starting the connection
SetTimeout (function () {$.ajax (setting);},3000);
}
};
$.ajax (setting);
}


2. Long connection

<?PHPOb_start();Echo str_repeat(' ', 4000);Ob_flush();Flush();$i= 0; while(true){   Echo $i, ' <br> '; Ob_flush(); Flush(); Sleep(3);}

PHP Customer Chat Answering system, long connection plus Ajax polling implementation

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.