Аякс долго опрашивает с Codeigniter и mysql

Я пытаюсь реализовать простое приложение чата, используя длинный опрос ajax с помощью codeigniter Framework. Ниже запрос ajax

var timestamp = null;
function check_incoming_chat_message(me, timestamp) {
$.ajax({
type: "Get",
dataType: 'json',
url: 'http://localhost/ci/index.php/chat/check_chatbox?to_me='+me+'&timestamp='+timestamp,
async: true,
//data: {to_me: me, timestamp: timestamp},
cache: false,
success: function(response) {
setTimeout(check_incoming_chat_message(response.to_chat, response.timestamp),10000);
},
error: function(response) {
setInterval(check_incoming_chat_message(response.to_chat, response.timestamp),2000);
},
complete: function() {
}
});

}

А ниже приведен код на стороне контроллера, в котором элемент управления продолжает циклически проверять базу данных, если только в mysql не появилось новое сообщение чата, и в этом случае оно выбрасывает сообщение чата в представление. И как только я получаю сообщение чата в представлении, я снова запускаю запрос ajax (по крайней мере, это моя идея)

function check_chatbox() {
$logged_in_user = $_GET["to_me"];
$last = $_GET["timestamp"];
header('Content-type: text/plain');
header('Content-type: application/json');
if($last == "" || $last == null) { // check if timestamp is null
$result = $this->chat_model->current_tstamp($logged_in_user);
foreach($result as $row)
{
$data = array('msg_time' => $row->msg_time,
'chat_message' => '',
'to_chat' => $logged_in_user
);
}
echo json_encode($data);
return;
}
$result = $this->chat_model->current_tstamp($logged_in_user);
foreach($result as $row)
{
$data_array = array('msg_time' => $row->msg_time);
}
$current = $data_array['msg_time'];while($current <= $last) {
usleep(100000);
//clearstatcache();
$result = $this->chat_model->current_tstamp($logged_in_user);
foreach($result as $row)
{
$data = array('msg_time' => $row->msg_time);
}
$current = $data['msg_time'];
}
$result = $this->chat_model->check_new_messages($logged_in_user, $current);
if($result) {
foreach($result as $row)
{
$data = array('chat_message' => $row->chat_message,
'from_chat' => $row->from_chat,
'chat_id'   => $row->chat_id,
'msg_time'  => $row->msg_time
);
}

echo json_encode($data);
return;
}
else {
return;
}
}

Проблема в том, что он блокирует все остальные запросы на странице, включая гиперссылки (как в тегах привязки в codeigniter). Может кто-нибудь помочь мне указать, что я делаю неправильно?

Спасибо

Примечание. Я пытаюсь повторить ту же процедуру, что и в этом примере.
http://www.mediafire.com/download/3xd0zx5ej3pp42b/comet.zip

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector