2017-07-25 194 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. 
}