Thursday, July 13th, 2006 |
|
SWF9 to SWF8 Communication - EI not LC - Part 1 |
It seems everywhere I’ve read when someone asks how to communicate between a version 9 SWF and a version 8 SWF that it loads, the answer is always “You have to use LocalConnection”. I’ve seen that response from Adobe people as well as others.
However, when I started looking into browser communication with my SWFs and then looked into the Flex-AJAX Bridge (FABridge), I thought maybe there was a better way. And there is: ExternalInterface.
The problem with LocalConnection is that using it means your communication between SWFs is asynchronous. For some types of communication this is ok, but most people want to do things like control a SWF8 from their SWF9 code. Because you can’t directly get or set properties or call methods of a loaded SWF8 from the SWF9 code something like LocalConnection just seems like such a pain. Plus you have to deal with multiple instances of a browser with your site being open, since channels need to be unique.
Typically you use ExternalInterface to communicate with the container, which in most cases is the browser. You pass data to and from JavaScript in a synchronous fashion.
To send data to JavaScript you use ExternalInterface.call, specify a JavaScript function you want to execute and then specify the arguments you want to send. To send data to Flash from JavaScript you need to setup a callback function in Flash using ExternalInterface.addCallback. The specified callback function can then be called directly by JavaScript, passing in the arguments you specify.
So what would be involved in getting data from Flash into JavaScript and back again?
import flash.external.ExternalInterface;
function fromJS(message:String):void
{
// data sent by JavaScript
}
// The names don't have to be the same but are here for simplicity
ExternalInterface.addCallback('fromJS', fromJS);
ExternalInterface.call('toJS', 'Hello JS');
In JavaScript we could write:
function toJS(message)
{
var fp = document.getElementById('EITest');
alert(message);
fp.fromJS('Hello Flash');
}
The above example would send a string to JavaScript, display it in an alert box, and then send a string back to Flash, where we could also display the data in an alert box or a textfield, etc.
So how does this help us with SWF9 to SWF8 communication? What you need to do is have both the SWF9 and the SWF8 setup ExternalInterface callbacks so they can each be talked to from JavaScript. Then when either SWF talks to JavaScript we can direct the message back to Flash into the other SWF’s callback.
In Part 2 you’ll see an example of this and I’ll explain why you don’t even need to write any JavaScript.









There is an even sweeter way to achieve this…
// in either swf
ExternalInterface.addCallback(”OneLinerBridge”, [target], oneLinerBridge);
ExternalInterface.call(”eval”, “document.getElementById(’swf_b’).registeredCallback(’var1′,’var2′)”);
*correction* - it would have made more sense had I put oneLinerBridge where registeredCallback is… but you get the jist. No more javascript includes!
Check out the link at the bottom of the article “Go on to Part 2″. There’s code that shows how to communicate with another SWF without any JavaScript. Part 1 was to explain the communication process.
The eval is a nice trick I’ve used before. It doesn’t make sense to use it here, since you can access the SWF directly and pass arguments, but it’s handy when you want to use Flash to manipulate the page it’s sitting on.