To enable connection statistics tracking, call the public method AjaxTCR.comm.stats
.collect(url) and pass the URL you are interested in sending the data to. The information
will be sent in a JSON structure when the page is unloaded.
collect : function (url) {
var sendConnectionStats = function(){
var results = AjaxTCR.comm.stats.get();
if (results.totalRequests > 0)
{
var payload = AjaxTCR.data.encodeJSON(results);
AjaxTCR.comm.sendRequest(url, {method:"POST",
payload:payload,
requestContentType:"application/json",
oneway:true});
}
260 P a r t I I : D e v e l o p i n g a n A j a x L i b r a r y
}; /* end callback function */
if(window.attachEvent)
window.attachEvent("onunload", sendConnectionStats);
else
window.addEventListener("unload", sendConnectionStats, false);
}
To see this in action, run the example at http://ajaxref.com/ch6/monitorconnectionstats.
html, open the link to the results in a new window, and then leave the primary window.
Refresh the results page and you should see the network statistics collected for your session
as demonstrated in Figure 6-17.
NOTE If you looked closely at the code presented, you??™ll notice a new options flag, oneway. This
indicates that the request is one way and thus not looking for a callback. Given that this request
is made upon unload, it will throw an error if thought to be bi-directional.
Pages:
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393