2017-07-19 78 views
0

我做的與電子應用的HTTP請求的響應時間,我有以下問題:延長電子

我需要一個HTTP請求從PHP接收數據,但超時應小於響應時間,並因此在交付任何東西給我之前取消請求。

任何人都知道如何延長http請求的等待時間?

我離開你的代碼

var http = require('http'); 
    var options = { 
     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

我假定你想延長節點http請求的超時時間,等待PHP服務器responde。

您可以以毫秒爲單位設置http請求的timeout屬性。從官方文檔

var http = require('http'); 
    var options = { 
     timeout: 1000, // timeout of 1 second 
     host: localStorage.getItem('server'), 
     port: localStorage.getItem('port'), 
     path: localStorage.getItem('directori') + '?nosession=1&call=ciberFiSessio&numSerie='+ localStorage.getItem("pc") 
    }; 
    http.get(options, ...) 

只需將屬性添加到您的選擇對象,例如

超時:一個數字,指定以毫秒爲單位的套接字超時。這將在套接字連接之前設置超時。

瞭解更多關於http.request(因爲它接受與http.get相同的選項)。