Flash 8 has a wonderful new feature called ExternalInterface, sometimes
referred to as External API. Essentially, it's the "new" way for the
Flash player to talk to it's host.
During the last Macromedia PodCast from Mike & Christian they had talked
about Flash and JavaScript communication with ExternalInterface. Now
that the cat is out of the bag on Studio 8, I wanted to let everyone
know that you can also use ExternalInterface with a desktop host as
well, and not just a browser.
What does this mean? It means we can now build an open source
synchronous projector (akin to mProjector) if we wanted to.
Heres some details: The communication is done via an event and a few
methods. When you call this ActionScript line:
result = ExternalInterface.call("TheMethod", "A sample string param...");
The ActiveX object will raise a "FlashCall" event and send you the event
request as an XML string. The string will look like this for the above
actionscript: <invoke name="TheMethod"
returntype="xml"><arguments><string>A sample string
param..."</string></arguments></invoke>
In the event handler, you can then fire off your "TheMethod" in your C#
(or C++,m or Python, or whatever is hosting the control), and then
return some data back to flash. When you return data, you have to pass
it back in the XML format as well, but you would call the
control.SetReturnValue("string") method, where "string" would look like
this:
"<string>This is a sample string being returned</string>"
The Flash movie will block until either the return value is set, or the
event handler is done executing (i.e. you don't have to return a value
if you don't want to).
Going the other way, from C# to Flash, you would first have to register
a method to call inside of the Flash movie. You can do that by a line
like this:
// expose a method "activeStateChange" to the C# container
ExternalInterface.addCallback("callMeFromCSharp", this, onCalled);
The params are the method name C# should use, and then the typical scope
/ callback in ActionScript. Then, you can call the method as so (in C#
here):
string result = flashMovie.CallFunction("<invoke
name=\"callMeFromCSharp\"
returntype=\"xml\"><arguments><bool>false</bool></arguments></invoke>");
Notice I'm using "CallFunction" to call the method, and I'm passing in
the XML string for the invocation. This will fire off the "onCalled"
ActionScript function inside of the movie. In that function then, I can
"return 'hello'" and then the C# result will be "<string>hello</string>".
So yes, this gives you synchronous communication from C# -> Flash and
Flash -> C#.
The awesome thing here is you don't have to use just strings. You can
send native objects back and forth via ExternalInterface. For instance,
if I want to return an array from an ExternalInterface call:
var listFiles:Array = ExternalInterface.call("ListFiles", "c:\windows");
I can send an array of strings back, by calling SetReturnValue with the
following string as the parameter:
"<array><property id='0'><string>test.dll<string></property><property
id='1'><string>hello.dll</string></property></array>"
That will get serialized by the Flash Player as an array, and then in
ActionScript I can loop over listFiles as an array (since it *is* an array):
for (var i = 0; i < listFiles.length; i++) // etc
Likewise, numbers are serialized as <number>12.123</number> and objects
look somewhat like arrays:
<object>
<property id='isThisCool'>
<bool>true</bool>
</property>
</object>
So, what the open source community should do is build a serialization /
deserialization library to abstract the XML format away. Then, there
should be an API build to abstract ExternalInterface away like:
var ftp:DudeFTP = DudeAPI.OpenFTPConnection(ip, username, password);
if (ftp.success) {
var files:Array = ftp.listFiles("/pub");
}
var newWindow:DudeWindow = DudeAPI.createWindow("some.swf", modal,
title, closeButton, this, onClose);
// etc.
Let the discussion begin! Maybe this is a good time to ressurect my
SharpFlash project... ;-)
-d
_______________________________________________
osflash mailing list
osflash@osflash.org
http://osflash.org/mailman/listinfo/osflash_osflash.org