Custom URLLoader with timeout.

I came across an issue yesterday loading ads from ad.doubleclick.net. For some reason, the crossdomain.xml file from ad.doubleclick.net was unavailable and the URLLoader class making the call eventually threw a Security Error, as the crossdomain policy is the first thing it looks for when making calls outside the SWF’s Security Domain.

This is easily handled by listening for Security Error Events from the URLLoader and additionally wrapping the load call in a try/catch statement. However, I was surprised to find out that URLLoader has no built-in timeout for altogether unavailable addresses. In most cases, a Security Error would be thrown when a crossdomain policy file fails to load, but that takes about 30 seconds. I’m still a little confused on the details of this, but check out the comments on the URLLoader in the AS3 Language Reference.

Enter URLLoaderDeluxe. I’m sure this has been done before, but I failed to find code out there that was ready to go. URLLoaderDeluxe extends URLLoader and adds an optional timeout value to the constructor.

In addition to handling non-existent or unavailable urls, turns out this aids in dealing with ad blocker plugins on browsers. The ad blocker will basically remap a call to a url it recognizes as an ad server, rendering it unavailable. In these cases, a simple timeout on the call can save you from errors, fail the load, and send the app on it’s way. Jesse Warden wrote on this a while ago, and his solution coincides with what I’ve done here.

Please let me know if this helps, if I’m totally off, or if you know anything else about this.

Comments