2017-07-25 3 views
0

Ich mache eine Anwendung mit electron.js und ich habe einen Punkt erreicht, wo ich eine http-Anfrage zum Aufruf eines PHP machen muss. Antwortzeit am Anfang war niedrig und die Anwendung fehlgeschlagen, bevor Daten empfangen wurden. Dann habe ich einen timeOut auf den HTTP-Pfad gesetzt, so dass die Wartezeit für die Serverantwort gestiegen ist. Das Problem esque ist, als ob der timeOut nicht war, nicht auf den angegebenen warten.Antwort Timeout auf HTTP-Anfrage

Kennen Sie irgendeinen Weg, um dieses Problem zu lösen?

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

Antwort

0

Verwenden node-fetch Bibliothek, die komplexere Optionen unterstützen, und arbeitet auch mit dem Versprechen, die für das Design Asynchron-Anwendungen moderner Ansatz ist. https://www.npmjs.com/package/node-fetch

Unterstützte Optionen (von Dokumentation, timeout enthalten)

{ 
    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. 
}