最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

nodejs發(fā)送http請求時(shí)遇到404長時(shí)間未響應(yīng)的解決方法

 更新時(shí)間:2017年12月10日 14:49:10   作者:yanglang1987500  
這篇文章主要為大家詳細(xì)介紹了nodejs發(fā)送http請求時(shí)遇到404長時(shí)間未響應(yīng)的解決方法

通常,我們在使用nodejs發(fā)送http請求時(shí),一旦遇到404響應(yīng),nodejs內(nèi)部會(huì)一直請求下去,直到超出它自己設(shè)定的響應(yīng)時(shí)長(最讓人惡心的地方就是這個(gè)時(shí)長還是沒法修改的。)很多人在這里碰到了麻煩。

我是在做arcgis地圖項(xiàng)目的時(shí)候,客戶提出需要使用天地圖提供的底圖服務(wù),當(dāng)時(shí)我直接使用silverlight客戶端的Arcgis API進(jìn)行http請求(同樣是內(nèi)部請求,不開源的東西就是這么讓人郁悶),同樣碰到了一個(gè)進(jìn)度條一直卡在那的問題。經(jīng)過調(diào)試發(fā)現(xiàn),是由于底圖加載請求超時(shí)的緣故,和nodejs一樣,silverlight一直在進(jìn)行請求直到超出它自己設(shè)定的響應(yīng)時(shí)限。

于是,我當(dāng)時(shí)正好有業(yè)余接觸nodejs,覺得這個(gè)東西性能應(yīng)該是不錯(cuò)的,至少比tomcat+java之流要好一些。于是,我著手寫了一個(gè)nodejs的代理服務(wù),用來請求天地圖的底圖。我當(dāng)時(shí)以為nodejs碰到404時(shí)能直接結(jié)束請求,但是呢,這個(gè)問題好像是行業(yè)規(guī)范似的,它竟然也和silverlight一樣不斷的請求……索性上網(wǎng)查了會(huì)資料,得出了以下這兩段代碼,解決了這個(gè)一直請求404的問題。

function proxyTDTMapData(img,level,row,col){ 
  var that = this,request = null,param = img.replace('_w',''); 
  var filename = tdtimgDir+'/'+img+'_'+level+'_'+row+'_'+col+'.png'; 
  path.exists(filename, function(exists) { 
    if (exists) { 
      readFileEntry(filename,that.res); 
    }else{ 
      var url = "http://t0.tianditu.com/"+img+"/wmts?service=wmts&request=GetTile&version=1.0.0&LAYER=" + param + "&tileMatrixSet=w&TileRow=" + row + "&TileCol=" + col + "&TileMatrix=" + level + "&style=default&format=tiles"; 
       
      httpGetWithTimeoutSupport(url,4000,function(response){ 
        //console.log("have a response!"); 
        if(200 == response.statusCode){ 
          var size = 0; 
          var chunks = []; 
          response.on('data', function(chunk){ 
            size += chunk.length; 
            chunks.push(chunk); 
          }); 
          response.on('end', function(){ 
            var data = Buffer.concat(chunks, size); 
            that.res.writeHead(200, { 
              'Content-Type' : 'image/png', 
              'Content-Length' : data.length, 
              'Accept-Ranges' : 'bytes', 
              'Server' : 'Microsoft-IIS/7.5', 
              'X-Powered-By' : 'ASP.NET' 
            }); 
            that.res.write(data, "binary"); 
            that.res.end(); 
            fs.writeFile(tdtimgDir+'/'+img+'_'+level+'_'+row+'_'+col+'.png', data); 
           }); 
        }else{ 
          readFileEntry(mapDir+"/null.png",that.res); 
        } 
      }).on("error",function(){ 
        readFileEntry(mapDir+"/null.png",that.res); 
      }); 
    } 
  }); 
   
   
 } 
function httpGetWithTimeoutSupport(options, timeout, callback) { 
  var timeoutEvent; 
 
  var req = http.get(options, function(res) { 
    res.on("end", function() { 
      clearTimeout(timeoutEvent); 
      // console.log("end"); 
    }) 
    res.on("close", function(e) { 
      clearTimeout(timeoutEvent); 
      // console.log("close"); 
    }) 
 
    res.on("abort", function() { 
      // console.log("abort"); 
    }); 
 
    res.on("error",function(){ 
      try{ 
        res.destory(); 
        clearTimeout(timeoutEvent); 
        //console.log("res error catch"); 
      }catch(e){ 
       
      } 
    }); 
    callback(res); 
  }); 
 
  req.on("timeout", function() { 
    //console.log("request emit timeout received"); 
    try{ 
      if (req.res) { 
        req.res.emit("abort"); 
      } 
      clearTimeout(timeoutEvent); 
      req.abort(); 
    }catch(e){ 
      //console.log("req timeout failed!"); 
    } 
  }); 
  req.on("error",function(){ 
    try{ 
      //console.log("req error catch"); 
    }catch(e){ 
     
    } 
  }); 
  timeoutEvent = setTimeout(function() { 
    try{ 
      req.emit("timeout"); 
    }catch(e){ 
      //console.log("timeout failed!"); 
    } 
  }, timeout); 
 
  return req; 
} 

其原理就是利用nodejs請求的幾個(gè)事件與計(jì)時(shí)器,一旦超出設(shè)定的響應(yīng)時(shí)長則立馬終結(jié)請求。如此,進(jìn)度條一直卡著的問題解決了。
細(xì)心的讀者可能看到了

path.exists(filename, function(exists) { 
    if (exists) { 
      readFileEntry(filename,that.res); 
    }else{...}); 

這段代碼,其實(shí)這里做了一下服務(wù)端的圖片緩存,一旦加載過的底圖圖片,直接從本地讀取,極大的加快了地圖的訪問速度(這個(gè)在效率上提升了至少10倍)。
至于Arcgis API for Silverlight 是如何實(shí)現(xiàn)天地圖底圖以及其它底圖服務(wù)(比如非標(biāo)準(zhǔn)墨卡托的地方坐標(biāo)系底圖服務(wù))加載的呢?請聽我下回分解。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

惠水县| 北流市| 治县。| 东方市| 礼泉县| 郓城县| 陵川县| 余江县| 都匀市| 习水县| 安平县| 托里县| 静海县| 阿克陶县| 清河县| 沁水县| 时尚| 千阳县| 丹寨县| 商丘市| 迁安市| 申扎县| 荥阳市| 遵化市| 河间市| 麟游县| 乌拉特前旗| 岳西县| 夏河县| 攀枝花市| 聂拉木县| 若尔盖县| 垦利县| 涿鹿县| 德化县| 上林县| 珲春市| 华宁县| 万荣县| 襄城县| 新安县|