The situation is like this: When first load the page, i made a long connection with the backend using ajax(loop at the backend without repsonse to the client). And the ajax request continue
running. And when i made another new ajax request, this new one will wait for the previuos one finish, then it
could run. is there any tips for this? or is there any yii configuration or server configuration for this?
If you need to make a second ajax only when the first one finishes… you can call the second one from the "complete" or "success" event of the first one…
make a ajax call to the backend action named actionA
$.ajax({… }). This call is mean to tell the php side to loop to read from the database, if there is something new, echo to frontend, otherwise continue looping to check. In frontend, there is also another
ajax call to submit message to the backend.
Backend:
Action controller/actionA (Sample):
<?php
if(isset($_GET[‘msg’])) {
// write into db
…
die();
}
$hasMsg = false;
while(!$hasMsg) {
// loop to check whether there is new msg
if($hasMsg) {
echo 'has new';
break;
}
usleep(1000000);
}
Yii::app()->end();
The problem is that when the page first loads, the first ajax is waiting for the php to check the new msg.
And if there is no new, it will continue checking. Meanwhile i make another new ajax call(submit msg to backend), this call will get the reponse until the first ajax finished. I used the default options for jquery.ajax and it should be async… I don’t know why this happened…
I think you should use CDbSession as app session. I read an article about that, but now i can not remember it. Try with it i think it should work.
Every time when you ask by ajax then framework runs session(opens session), and next time the opening session will be waited until first request has finished.