jmhobbs

Data throughput with ExternalInterface

I've started tinkering with Flash and AS3, and I've used the ExternalInterface a lot. For one project the amount of data to be pushed into JavaScript from AS3 was highly variable, could be huge.

I couldn't find a good reference for how much it can handle, so I threw this together. In the end I made it to ~10Mb through at a time in both Firefox and Chrome. No IE testing :-)

The basic idea was to keep requesting large chunks until it broke, then go back and slowly work my way up to the max.

The JavaScript:

function $(e) { return document.getElementById(e); }

var expect = 0;
var interval = 100000;

function callback (bytestring) {
  $('logarea').innerHTML = 'Expect: '+expect+'
'; $('logarea').innerHTML = 'Got: '+bytestring.length+'
'; if(expect == bytestring.length) { expect += interval; } else { if(interval == 1) { $('logarea').innerHTML += 'Final Value:'+bytestring.length+'
'; return; } else { expect -= interval; interval = interval / 10; expect += interval; } } if(bytestring.length >= 10000000) { // ~10 Mb $('logarea').innerHTML += 'Reached ~10 Mb, I give up.
'; return; } setTimeout("$('eiEmbed').expect(expect)", 50); }

The ActionScript:
import flash.external.ExternalInterface;

if(!ExternalInterface.available)
  trace('EI Not Available. What have you done?!');

var returnval:String = '';
var lastexpect:int = 0;

function expect (bytes:int):void {
  var toadd:int = bytes;

  if(bytes < lastexpect)
    returnval = '';
  else
    toadd = bytes - lastexpect;

  for(var i = 0; i < toadd; ++i) {
    returnval += "a";
  }
  lastexpect = bytes;
  ExternalInterface.call('callback', returnval);
}
ExternalInterface.addCallback('expect', expect);

You can run it yourself here. Or download it here.