为什么我的 XHR 调用等待对方返回响应


Why are my XHR calls waiting for each other to return a response

我在一个页面中有一个iframe,它不断地轮询服务器以查找由"主"XHR主动更新的会话变量。

所以基本上:

  1. 主 XHR 运行并执行其操作,在运行时更新会话变量。通常需要一段时间,比如说超过10秒。

  2. 当主 XHR 运行时,我使用并行 XHR 请求轮询服务器以查找相同的会话变量。每当我从轮询 XHR 获得响应时,我都应该更新前端视图。

问题是轮询 XHR 直到

主 XHR 完成之后才会返回任何内容,此时它们当然已经毫无用处了。这真的是处理会话时的预期行为吗?像每个客户端连接一个会话之类的限制?

编辑:

下面是一些代码片段。代码非常大,所以我试图将其精简为最基本的内容。它可能有一些语法错误,因为我刚刚从源代码中取出了重要的部分。

生成内嵌框架

(function($) {
    $(document).on('click','#proceed_form',function(){
        $('#upload_frame').show(); 
        function set () { 
            $('#upload_frame').attr('src','/productUpload/generateIframe'); 
        }
        setTimeout(set); 
    });
});

内嵌框架

<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(或脚本结束)时释放它。同时其他脚本尝试访问会话,等待发布。详细文章在这里或在手册会话-写-关闭