【问题标题】:WinJS.xhr Timeout Loses Requests?WinJS.xhr 超时丢失请求?
【发布时间】:2012-12-29 19:38:25
【问题描述】:

我想要做的(尽管I fully suspect there's a better way 这样做)是将HTTP 请求发送到我网络上的一系列主机。我可以通过循环调用WinJS.xhr 来访问每个主机。但是,完成该范围所需的时间太长。

在 Fiddler 中检查表明一次发送了十几个请求,等待超时,然后继续处理下一个左右的请求。所以我想我会尝试减少每个请求的超时时间。根据我的需要,如果主机在 500 毫秒内没有响应,它就不会响应。

the documentation 之后,我尝试使用足够小的设置将对WinJS.xhr 的调用封装在对WinJS.Promise.timeout 的调用中,但没有任何变化。更改承诺超时并没有真正影响实际请求。

多一点搜索让我找到了a suggestion,我可以修改WinJS.xhr 使用的XMLHttpRequest 对象并设置超时。在以更快的速度发出请求方面,这就像一个魅力。但是,似乎有副作用。

查看 Fiddler 中的请求,大约有十几个会很快触发,然后整个事情就结束了。 “接下来的十几个”永远不会到来。 有时(基于异步调用的半随机性)fiddler 中出现的前十个左右包括范围的低端和范围的 9-10 和高端的 2-3范围,或接近它。

还有什么我可以尝试的,或者其他方式来实现这里的最终目标吗? (在这个问题的范围内,最终目标是在合理的时间内发送大量请求,但也欢迎任何关于更好的整体方式扫描网络上特定服务的建议。)

【问题讨论】:

    标签: timeout xmlhttprequest winjs


    【解决方案1】:

    你能写出你用于超时的代码吗,我写了这样的东西但它不起作用,所以我很好奇你是怎么做的:

        var timeoutFired = function () {
            console.log("derp");
        };
    
        var options = {
            url: "http://somesite.com",
            responseType: "document",
            customRequestInitializer: function (req) {
                req.timeout = 1;
                req.ontimeout = timeoutFired;
                //do something with the XmlHttpRequest object req
             }
        };
    
        WinJS.xhr(options).
        ....
    

    以下是一些您可能会发现有用的替代方法,不确定超时如何/为什么不起作用,但我尝试编写自定义超时函数:

    (function (global) {
        var options = {
            url: "http://something.com",
            responseType: "document",
        };
    
        var request = WinJS.xhr(options).then(
            function (xmlHttpRequest) {
                console.log("completed");
            },
            function (xmlHttpRequest) {
                //error or cancel() will throw err
                console.log("error"+ xmlHttpRequest.message);
    
            },
            function (xmlHttpRequest) {
                console.log("progress")
        });  
    
        function waitTime() {
            return new WinJS.Promise(
                function (complete, error, progress) {
                    var seconds = 0;
                    var interval = window.setInterval(
                        function () {
                            seconds++;
                            progress(seconds);
                            //prob should be called milliseconds
                            if (seconds > 5) {
                                window.clearInterval(interval);
                                complete();
                            }
                        }, 100);
                });
        };
    
        waitTime().done(
            function () {
                console.log("complete");
                request.cancel();
            },
            function () {
                console.log("error")
            },
            function (seconds) {
                console.log("progress:" + seconds)
            });
    });
    

    另一个很酷的小技巧是使用 promise.any (vs .join),它会在一个 OR 另一个先完成时触发,因此考虑到这一点,您可以编写如下内容:

     (function (global) {
        var options = {
            url: "http://url.com",
            responseType: "document",
        };
    
        var request = {
            runRequest: function () {
                return WinJS.xhr(options).then(
                function (xmlHttpRequest) {
                    console.log("completed");
                },
                function (xmlHttpRequest) {
                    //error or cancel() will throw err
                    console.log("error" + xmlHttpRequest.message);
    
                },
                function (xmlHttpRequest) {
                    console.log("progress")
                });
            }
        };
    
        WinJS.Promise.any([WinJS.Promise.timeout(500), request.runRequest()]).done(
            function () {
                console.log("any complete");
            });
    })();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 2011-02-26
      • 1970-01-01
      • 2014-03-03
      • 2015-02-14
      相关资源
      最近更新 更多