2017-07-25 140 views
0

我正在用electron.js做一個應用程序,並且我已經達到了需要發出http請求來調用php的程度。開始時的響應時間很短,應用程序在接收數據之前失敗。然後,我給HTTP路徑放了一段時間,以便服務器響應的等待時間增加。問題就像是timeOut不是,不等待指定的那個。對http請求的響應超時

你知道解決這個問題的方法嗎?

var http = require('http'); 
    var options = { 
     timeout: 50000, 
     host: localStorage.getItem('server'), 
     port: localStorage.getItem('port'), 
     path: localStorage.getItem('directori') + '?nosession=1&call=ciberFiSessio&numSerie='+ localStorage.getItem("pc") 
    }; 
http.get(options, function(res) { 
     alert("hola"); 
     if (res.statusCode == 200){ 
     //reinicia(); 

     res.on('data', function (chunk) { 
      str = chunk; 
      alert(str); 

      var myJSON = JSON.parse(str); 
      //alert(myJSON.fi); 

      if(parseInt(myJSON.fi)==0){ 
      alert("Hi ha hagut un problema!"); 
      }else{ 
      reinicia(); 
      } 

     }); 

     }else{ 
     alert("El lloc ha caigut!"); 
     alert(res.statusCode); 
     } 
    }).on('error', function(e) { 
     alert("Hi ha un error: " + e.message); 
    }); 

回答

0

使用node-fetch庫,它支持更復雜的選項,並且還與承諾,這是設計異步應用現代的方法。 https://www.npmjs.com/package/node-fetch

支持的選項(從文檔,timeout包括)

{ 
    method: 'GET' 
    , headers: {}  // request header. format {a:'1'} or {b:['1','2','3']} 
    , redirect: 'follow' // set to `manual` to extract redirect headers, `error` to reject redirect 
    , follow: 20   // maximum redirect count. 0 to not follow redirect 
    , timeout: 0   // req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies) 
    , compress: true  // support gzip/deflate content encoding. false to disable 
    , size: 0   // maximum response body size in bytes. 0 to disable 
    , body: empty  // request body. can be a string, buffer, readable stream 
    , agent: null  // http.Agent instance, allows custom proxy, certificate etc. 
}