This code shows a sample of how one can make multiple simultaneous web requests using the XMLHttpRequest object in javascript.
It does this by creating a wrapper class that manages a pool of pfsRq request objects that handle the request / response to various
URLs.
To use this sample, you must create an HTML page containing a button which invokes the DoRequest function in the button onclick event. When the sample is run, it simply displays the returned xml in an alert dialog window.
NOTE: Cross Domain Requests are prohibited using XMLHttpRequest object. What that means is that you can only make requests to your local domain. (ie same network). However, if the server you are trying to access returns an Access-Control-Allow-Orign * header you can make a cross domain request. More often than not though, the server you are trying to reach does not return this header so the request will fail.
Using the Class
The pfsRqMgr class
The pfsRqMgr class performs all the logic necessary to make multiple web requests to multiple web sites. This class makes use of the pfsRq object (class)
To use this sample, you must create an HTML page containing a button which invokes the DoRequest function in the button onclick event. When the sample is run, it simply displays the returned xml in an alert dialog window.
NOTE: Cross Domain Requests are prohibited using XMLHttpRequest object. What that means is that you can only make requests to your local domain. (ie same network). However, if the server you are trying to access returns an Access-Control-Allow-Orign * header you can make a cross domain request. More often than not though, the server you are trying to reach does not return this header so the request will fail.
Using the Class
// define global variable var rqMgr = new pfsRqMgr(); // ------------------------------- // Do The Web Request // ------------------------------- function DoRequest() { var sites = new Array("http://w1.weather.gov/xml/current_obs/KPVD.rss", "http://w1.weather.gov/xml/current_obs/display.php?stid=KSFO", "http://w1.weather.gov/xml/current_obs/KBOS.rss"); for (var i = 0; i < sites.length; i++) { // get a new request var rq = rqMgr.getNew(); // set the url rq.url = sites[i]; rq.ahdr("Accept", "text/html"); rq.ahdr("Origin",""); // set a passthru variable that will carry through // the web request rq.passthru[0] = i; // set the return handler to the same function for each // call rq.responseHandler = DoRequestResponseHandler; // send the response rq.get(); } } // ------------------------------- // Handle the Web Request Response // ------------------------------- function DoRequestResponseHandler(rqst) { if (!rqst.error) { var prefix = "UNDEF"; switch(rqst.passthru[0]) { case 0: prefix = "PROVIDENCE"; break; case 1: prefix = "SAN FRANCISCO"; break; case 2: prefix = "BOSTON"; break; } alert(prefix + ": " + rqst.xtext); } // dispose rqMgr.dispose(rqst); }
The pfsRqMgr class
The pfsRqMgr class performs all the logic necessary to make multiple web requests to multiple web sites. This class makes use of the pfsRq object (class)
// ********************************************************** // This Request Manager class manages a pool of 10 web requests // that can be run simultaneously. // ********************************************************** function pfsRqMgr(nSize) { this.size = 10; // first, determine nSize; if (nSize != undefined) { if (nSize != null) { if (!isNaN(nSize)) { if (nSize > 0) { this.size= nSize; } } } } // allocate the array this.rq = new Array(this.size); // now define an array which will hold // request objects for (var i = 0; i < this.rq.length; i++) { this.rq[i] = null; } // ------------------------------------------------ // this gets a new request object // ------------------------------------------------ this.getNew = function() { var rt = null; for (var i = 0; i < this.size; i++) { if (this.rq[i] == null) { this.rq[i] = new pfsRq(); this.rq[i].id = i; rt = this.rq[i]; break; } } return rt; } // ------------------------------------------------- // dispose the request object // ------------------------------------------------- this.dispose = function(rqst) { var ndx = -1; if (rqst != undefined) { if (rqst != null) { if (!isNaN(rqst.id)) { if (rqst.id >= 0 && rqst.id < this.size) { if (this.rq[rqst.id] != null) { this.rq[rqst.id] = null; } } } } } } // ----------------------------------------------- // clear clears all the connections // ----------------------------------------------- this.disposeAll = function () { for (var i = 0; i < this.size; i++) { this.dispose(this.rq[i]); } } } // END RQMGR