我在页面中有一个 iframe,它不断轮询服务器以获取由“主”XHR 主动更新的会话变量。
所以基本上:
主 XHR 运行并执行其任务,在运行时更新会话变量。通常需要一段时间,比如说10秒以上。
当主 XHR 运行时,我使用并行 XHR 请求轮询服务器以获取相同的会话变量。每当我收到轮询 XHR 的响应时,我都应该更新前端视图。
问题是轮询 XHR 在主 XHR 完成之前不会返回任何内容,此时它们当然已经毫无用处了。这真的是处理会话时的预期行为吗?比如每个客户端连接一个会话之类的限制?
EDIT:
这是一些代码片段。代码相当大,所以我尝试将其精简为最基本的内容。它可能有一些语法错误,因为我刚刚从源代码中取出了重要部分。
生成 iframe
(function($) {
$(document).on('click','#proceed_form',function(){
$('#upload_frame').show();
function set () {
$('#upload_frame').attr('src','/productUpload/generateIframe');
}
setTimeout(set);
});
});
Iframe
<script type='text/javascript' src="/assets/js/src/vendor/jquery-1.9.1.js" ></script>
<script>
(function($) {
$(document).ready(function() {
setInterval(function()
{
$.get("/productController/getProgress", function(data)
{
$('#progress_container').fadeIn(100); //fade in progress bar
$('#progress_bar').width(data +"%"); //set width of progress bar based on the $status value (set at the top of this page)
$('#progress_completed').html(parseInt(data) +"%"); //display the % completed within the progress bar
}
)},500);
});
})(jQuery);
</script>
<div id="progress_container">
<div id="progress_bar">
<div id="progress_completed"></div>
</div>
</div>
PHP应用程序
class productUpload extends CI_Controller{
/**
* Respond to XHR poll request
*
*/
public function getUploadedBytesToCloud()
{
session_start();
$uploadedBytes = $_SESSION['bytes_uploaded'];
echo json_encode(['uploadedBytes' => $uploadedBytes]);
}
/**
* Main controller action
* Uploads the images of a product to the cloud
*
*/
public function moveProductImagesToCloud($productId)
{
/**
* Some logic to get the product image directory
*
*/
$productPath = '/assets/product/image_dir';
$directoryMap = directory_map($productPath);
foreach($directoryMap as $key => $file){
/**
* Upload file to AWS S3 bucket
*/
$this->awsUploader->uploadFile(...);
$fileSize = $_SESSION['bytes_uploaded'];
$fileSize += filesize(getcwd()."/".$productPath."/".$file);
$_SESSION['bytes_uploaded'] = fileSize;
}
}
}
是的,默认会话管理器(使用文件)会在您执行 session_start 时锁定会话文件,并在您执行 session_write_close (或脚本结束)时释放它。同时其他脚本尝试访问会话,等待释放。一篇详细的文章here http://konrness.com/php5/how-to-prevent-blocking-php-requests/或在手册中会话-写入-关闭 http://php.net/manual/en/function.session-write-close.php
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)