2012-12-29 88 views
0

我正在嘗試做的事(儘管I fully suspect there's a better way這樣做)是向我的網絡上的一系列主機發送HTTP請求。我可以通過循環呼叫WinJS.xhr來擊中每個主機。但是,完成範圍需要很長的時間。WinJS.xhr超時失去請求?

在Fiddler中檢查顯示,一次發送十幾個請求,等待超時,然後繼續前進到下一打。所以我想我會盡量減少每個請求的超時。根據我的需要,如果主機在500毫秒內沒有響應,則不會響應。

正在關注the documentation,我試着打電話給WinJS.xhr打電話給WinJS.Promise.timeout,但是這個設置足夠小,但沒有任何變化。更改承諾超時並不會真正影響實際請求。

更多的搜索引導我到a suggestion,從而我可以修改WinJS.xhr使用的XMLHttpRequest對象,並設置超時值。就以更快的速度爆發請求而言,這起到了很大的作用。但是,似乎有一個副作用。

看着小提琴手的要求,大約十幾個人很快起火,然後整個事情就結束了。 「下十二個左右」永遠不會來。 有時候(基於異步調用的半隨機性)第一個十幾個出現在提琴手中包含9-10個從低位和範圍以及2-3個位於範圍的頂端,或者接近於它。

還有什麼我可以嘗試,或者其他方式來完成這裏的最終目標? (在這個問題的範圍內,最終目標是在合理的時間內發送大量請求,但也歡迎任何有關更好地總體掃描網絡上特定服務的建議。)

回答

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。加入),其觸發時關閉一個或其他飾面第一,所以考慮到這一點,你可以寫這樣的事情:

(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"); 
     }); 
})();