沃梦达 / 编程问答 / php问题 / 正文

长轮询锁定其他 AJAX 调用

Long polling locking up other AJAX calls(长轮询锁定其他 AJAX 调用)

本文介绍了长轮询锁定其他 AJAX 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望进行长轮询以将一些数据推送"到客户端,并且我还在长轮询的同时对服务器进行其他不相关的 AJAX 调用.在长轮询收到响应(来自响应或超时)之前,我的其他 AJAX 调用似乎不会完成.当我单步执行 Javascript 时,似乎在适当的时间发送了第二个 AJAX 请求,但直到长轮询请求得到响应后才收到响应.知道发生了什么吗?

I'm looking to do long polling to "push" some data down to the client and I'm also making other unrelated AJAX calls to the server in parallel with the long polling. It appears that my other AJAX calls won't complete until the long poll has received a response (either from a response or timeout). When I step through the Javascript, it appears that the 2nd AJAX request is being sent at the proper time, but the response isn't being received until the long poll request gets a response. Any idea what is going on?

这是长轮询部分的代码:

Here is the code for the long polling portion:

服务器端:

    function getPlaylistTracksIfChanged($playlist_id, $numClientTracks) {
  $reportChange = false;
  for($i = 0; $i < 10; $i++) {
   $numServerTracks = $this->PlaylistTrack->find('count', array(
     'conditions' => array('playlist_id' => $playlist_id)
    )
   );

   if($numClientTracks != $numServerTracks) {
    $reportChange = true;
    break;

   }

   sleep(3);

  }

  if($reportChange) {
   $playlist_tracks = $this->PlaylistTrack->find('all', array(
    'conditions' => array('playlist_id' => $playlist_id),
    'order' => array('PlaylistTrack.position') 
    )
   );

   $this->set('playlist_tracks', $playlist_tracks);
   $this->layout = false;
   $this->render('show_playlist_tracks_list');

  } else {
   $this->autoRender = false;
   return 'false';
  }

 }

客户端:

function checkForChangesOnServer() {
 $.post('/getResultsIfChanged/' + playlist_id + '/' + $('#sortable_tracks').children().size(), function(results) {

  if(results == 'false') {
   //alert('no change');
  } else {
   //alert('change');
  }

  checkForPlaylistChangesOnServer();

 });
}

以及另一个 AJAX 调用的示例:

And a sample of another AJAX call:

服务器端:

    function getLibraryTracksStartingWithLetter($user_id, $letter) {
  $results = $this->Track->find(
   'all',
   array(
    'conditions' => array(
     'user_id' => $user_id,
     'OR' => array(
      'Track.artist LIKE' => $letter . '%',
      'Track.name LIKE' => $letter . '%'
     )
    ),
    'order' => array('case when Track.artist = "" then 1 else 0 end', 'Track.artist', 'Track.name')
   )
  );

  $this->set('results', $results);
  $this->layout = false;
  $this->render('show_library_results_list');
 }

客户端:

    function loadLibraryResultsForLetter(letter) {
 highlightLetterFilter(letter);

 $.post('/getLibraryTracksStartingWithLetter/' + user_id + '/' + letter, function(results) {
  updateLibraryResults(results);
 });
}

推荐答案

您好像遇到了会话文件锁定.

Seems like you experienced the session file lock.

在ajax端点的开头执行session_write_close()(或cakephp中的相应函数)关闭会话.

Perform session_write_close() (or corresponding function in cakephp) to close the session in the begin of the ajax endpoint.

这篇关于长轮询锁定其他 AJAX 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:长轮询锁定其他 AJAX 调用

基础教程推荐