I live in Omaha.
 
Navigation
 
Search
 
Random Image
100
 
Me. Elsewhere.
 
Archives
 
Darcy
 
Recently Read
 
Things I Like
Amarok Media Player
 
License
 
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+'<br/>';
  $('logarea').innerHTML = 'Got: '+bytestring.length+'<br/>';
  if(expect == bytestring.length) {
    expect += interval;
  }
  else {
    if(interval == 1) {
      $('logarea').innerHTML += '<b>Final Value:'+bytestring.length+'</b><br/>';
      return;
    }
    else {
      expect -= interval;
      interval = interval / 10;
      expect += interval;
    }
  }
 
  if(bytestring.length >= 10000000) { // ~10 Mb
    $('logarea').innerHTML += '<b>Reached ~10 Mb, I give up.</b><br/>';
    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.

Posted July 16th, 2009 - Permalink
Categories: Geek
Tags: , , ,
You can leave a comment, or trackback from your own site.
 
Possibly Related Posts
 
Adjacent Posts
 
Comments
 
Copyright © 2006 - 2010 John Hobbs
get userping