为什么我的XHR呼叫等待对方返回响应 [英] Why are my XHR calls waiting for each other to return a response

查看:123
本文介绍了为什么我的XHR呼叫等待对方返回响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,是不断地轮询服务器为正在积极更新由一个主XHR会话变量中的iframe。

所以基本上:

  1. 主要XHR运行并做它的事情,因为它运行更新会话变量。通常需要一段时间,比如说10秒以上。

  2. 虽然主XHR运行时,我轮​​询服务器使用并行XHR请求相同的会话变量。我应该更新前端视图每当我从我的投票XHRs的响应。

问题是,投票XHRs不返回任何东西,直到主XHR完成后,在这一点上,他们已经无用的课程。这是真的时,与会话处理预期的行为?像每个客户端连接类型的限制的一个会议?

编辑:

下面是一些code段。在code是pretty的大,所以我想修剪下来到最基本的要素。它可能具有的类型在这里,我只是把从我的源$ C ​​$ c中的重要组成部分的一些语法错误。

生成的iframe

 (功能($){
    $(文件)。在('点击','#proceed_form',函数(){
        $('#upload_frame)显示()。
        功能集(){
            $('#upload_frame)ATTR('src'中,'/ productUpload / generateIframe')。
        }
        的setTimeout(套);
    });
});
 

iframe的

 <脚本类型=文/ JavaScript的SRC =/资产/ JS / src目录/供应商/ jQuery的-1.9.1.js>< / SCRIPT>

<脚本>

(函数($){

    $(文件)。就绪(函数(){
        的setInterval(函数()
        {
            $获得(/ ProductController的/ getProgress,功能(数据)
            {
                $('#progress_container')淡入(100)。 //淡入进度条
                $('#progress_bar')宽度(数据+%);进度条//设置宽度基于$状态值(设置在这个页面的顶部)
                $('#progress_completed')的HTML(parseInt函数(数据)+%)。 //显示进度条内完成%
            }
        )},500);

    });

})(jQuery的);

< / SCRIPT>


< D​​IV ID =progress_container>
    < D​​IV ID =progress_bar>
         < D​​IV ID =progress_completed>< / DIV>
    < / DIV>
< / DIV>
 

PHP应用程序

 类productUpload扩展是CI_Controller {

    / **
     *响应XHR轮询请求
     *
     * /
    公共职能getUploadedBytesToCloud()
    {
        在session_start();
        $ uploadedBytes = $ _SESSION ['bytes_uploaded'];
        回声json_en code(['uploadedBytes'=> $ uploadedBytes]);
    }

    / **
     *主控制器操作
     *上传某个产品的图片到云
     *
     * /
     公共职能moveProductImagesToCloud($ productId参数)
     {
          / **
           *有些逻辑,以获得产品形象目录
           *
           * /
           $ productPath ='/资产/产品/ image_dir;
           $ directoryMap = directory_map($ productPath);
           的foreach($ directoryMap为$关键=> $文件){
                 / **
                  *上传文件到AWS S3存储
                  * /
                 $这个 - > awsUploader-> uploadFile(...);

                 $档案大小= $ _SESSION ['bytes_uploaded'];
                 $作品尺寸+ =文件大小(GETCWD()/$ productPath/$文件。);
                 $ _SESSION ['bytes_uploaded'] =文件大小;
            }
      }

}
 

解决方案

是的,默认的会话管理器(使用文件)锁定会话文件当你在session_start并释放它,当你做session_write_close(或者脚本结束)。同时其他脚本试图访问会话,等待释放。详细文章这里或手动< A HREF =htt​​p://php.net/manual/en/function.session-write-close.php相对=nofollow>会话写靠近

I have an iframe within a page that is continually polling the server for a session variable that is being actively updated by a "main" XHR.

So basically:

  1. Main XHR runs and does its thing, updating a session variable as it runs. Usually takes a while, say more than 10 seconds.

  2. While main XHR is running, I poll the server for the same session variable using parallel XHR requests. I'm supposed to update the front-end view whenever I get a response from my polling XHRs.

The problem is that the polling XHRs don't return anything until after the main XHR completes, at which point they are already useless of course. Is this really the expected behavior when dealing with sessions? Something like one session per client connection kind of restriction?

EDIT:

Here's some code snippet. The code is pretty big so I tried to trim it down to the bare essentials. It may have some syntax error as typed here as I just took out the important parts from my source code.

Generate 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 Application

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;
            }
      }

}

解决方案

Yes, default session manager (using files) locks the session file when you do session_start and releases it when you do session_write_close (or the scripts ends). Meanwhile other scripts trying to access session, wait to the release. a detailed article here or at the manual session-write-close

这篇关于为什么我的XHR呼叫等待对方返回响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆