Ask for an Ajax long connection about the problem.

Source: Internet
Author: User
Ask for an Ajax long connection problem.
Younger brother is doing a similar WEBQQ chat Web page, originally through Ajax every two seconds to send a request to obtain information, now want to change a message sent out, if no new message server is not returned, encountered a new message returned by the server, the client sends a request again,
But the implementation of the process found that when the message is waiting, the new creation of other AJAX requests are also waiting, it seems to wait for this return, the other to continue to execute, but after Ajax created an object, it should not be OK with each other AH? I'm using jquery ajax.
This is what I wrote on this side of PHP:
while (($res->num_rows) ==0) {
$res->close ();
Sleep (1);
$res = $mysqli->query ($sql);
}

When you comment out this while, the client does not have any problems with sending a request every 2s
Client code:
function Get_msg () {
$.post ("/lts/class/get_msg.php", "lst_msg=" +lst_msg,function get_msg_callback (msg_back) {
var i=0;
while (msg_back[i].msg!= "") {
$ ('. Msgs_box '). Append (" +msg_back[i].sender+": "+msg_back[i].msg+" ");
Lst_msg=msg_back[i].id;
i++;
}
Call Now
Get_msg ();
}, "JSON");
Return
}

------Solution--------------------
It's your session.

It is recommended to Session_write_close () immediately after the page Session_Start without session changes.
  • 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.