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