会社の erp に Facebook に似た通知システムを作成したいと考えています。良好なパフォーマンスを維持するために、私は長いポーリングを使用します - ループされた ajax クエリ php スクリプトを秒数使用します。ERP 内の別のページに移動しようとするまで、すべて正常に動作します。ページ上のリンクをクリックすると、手動で ajax 接続を強制終了した場合でも、バックグラウンドの php スクリプトが完了するまで待機してすべてがフリーズします。JS スクリプトはすべてのページに含まれており、ページの読み込み時に自動的に開始されます。
function notificationsObject(){
var nl = new Object();
nl.startAjax = function(data){
if(nl.ajaxObject != null) try{ nl.ajaxObject.abort() } catch(e){} finally{nl.ajaxObject = null}
nl.ajaxObject = $.ajax({
url: nl.ajaxUrl, //declared before function declaration
type: 'POST',
data: {data: data}
}).done(function(responseText){nl.ajaxSuccess(responseText)
}).fail(function(responseText){nl.ajaxFail(responseText)});
}
nl.ajaxSuccess = function(response){
console.debug(response);
nl.startAjax();
}
nl.ajaxFail = function(response){
//@todo some code here
}
nl.killConnection = function(){
if(nl.ajaxObject != null) try{ nl.ajaxObject.abort() } catch(e){} finally{nl.ajaxObject = null}
console.debug('killing');
}
(more code here)
return nl;
}
初期化コードは次のようになります
$(document).ready(function(){
var notifications = notificationsObject();
notifications.startAjax({name: 'startup'});
setTimeout(function(){window.onbeforeunload = function(){notifications.killConnection()};}, 1000);
});
また、いくつかの PHP コードもあります。
public function executeUsersNotificationListener(){
ignore_user_abort(false);
ob_end_flush();
$this->getResponse()->setHttpHeader("Cache-Control", "no-cache");
$this->getResponse()->setHttpHeader("Pragma", "no-cache");
$this->getResponse()->setHttpHeader("Expires", 0);
$timeLimit = 30;
set_time_limit($timeLimit+1);
echo 'test';
$i = 0;
while($i++ < $timeLimit){
echo " ";
sleep(1);
}
return sfView::NONE;
}
上記のように、いくつかの調査と使用ignore_user_abort
などを行いましたが、機能しません。