Usually, when we use nodejs to send http requests, once we encounter a 404 response, nodejs will continue to request until it exceeds the response time it sets itself (the most disgusting thing is that this time still cannot be modified.) Many people are in trouble here.
When I was working on the arcgis map project, the client asked to use the base map service provided by Tianma Map. At that time, I directly used the Arcgis API of the silverlight client to make http requests (the same is an internal request, and things that are not open source are so depressing), and I also encountered a problem of a progress bar stuck there. After debugging, it was found that the basemap loading request timed out. Like nodejs, silverlight continues to make requests until it exceeds its own response time limit.
So, I happened to have an amateur contact with nodejs at that time and thought that the performance of this thing should be good, at least better than tomcat+java and other streams. So, I started writing a nodejs proxy service to request the base map of the Tianma map. At that time, I thought nodejs could directly end the request when it encountered 404, but this problem seemed to be an industry standard, and it actually kept requesting like silverlight... I simply searched the Internet and found the following two codes, which solved the problem of constantly requesting 404.
function proxyTDTMapData(img,level,row,col){ var that = this,request = null,param = ('_w',''); var filename = tdtimgDir+'/'+img+'_'+level+'_'+row+'_'+col+'.png'; (filename, function(exists) { if (exists) { readFileEntry(filename,); }else{ var url = "/"+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){ //("have a response!"); if(200 == ){ var size = 0; var chunks = []; ('data', function(chunk){ size += ; (chunk); }); ('end', function(){ var data = (chunks, size); (200, { 'Content-Type' : 'image/png', 'Content-Length' : , 'Accept-Ranges' : 'bytes', 'Server' : 'Microsoft-IIS/7.5', 'X-Powered-By' : '' }); (data, "binary"); (); (tdtimgDir+'/'+img+'_'+level+'_'+row+'_'+col+'.png', data); }); }else{ readFileEntry(mapDir+"/",); } }).on("error",function(){ readFileEntry(mapDir+"/",); }); } }); }
function httpGetWithTimeoutSupport(options, timeout, callback) { var timeoutEvent; var req = (options, function(res) { ("end", function() { clearTimeout(timeoutEvent); // ("end"); }) ("close", function(e) { clearTimeout(timeoutEvent); // ("close"); }) ("abort", function() { // ("abort"); }); ("error",function(){ try{ (); clearTimeout(timeoutEvent); //("res error catch"); }catch(e){ } }); callback(res); }); ("timeout", function() { //("request emit timeout received"); try{ if () { ("abort"); } clearTimeout(timeoutEvent); (); }catch(e){ //("req timeout failed!"); } }); ("error",function(){ try{ //("req error catch"); }catch(e){ } }); timeoutEvent = setTimeout(function() { try{ ("timeout"); }catch(e){ //("timeout failed!"); } }, timeout); return req; }
The principle is to use several events and timers for nodejs requests, and once the set response time exceeds the set time, the request will be terminated immediately. In this way, the problem of the progress bar being stuck has been solved.
Careful readers may see
(filename, function(exists) { if (exists) { readFileEntry(filename,); }else{...});
In fact, this code has been used to cache the image on the server side. Once the loaded base map image is read directly from the local area, greatly speeding up the access speed of the map (this has increased efficiency by at least 10 times).
As for how Arcgis API for Silverlight implements loading of Tianma base maps and other base map services (such as non-standard Mercator local coordinate system base map services)? Please listen to my next breakdown.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.