You are missing some Flash content that should appear here! Perhaps your browser cannot display it, or maybe it did not initialise correctly.
Async Flash Studio Tutorial - Write a sample extension (Part 1)
Introduction
Async Flash Studio(AFS) offers interfaces to extend its functions. There are two kinds of extensions, synchronous and asynchronous. Since synchronous extension may block UI actions, asynchronous extension is the recommended kind of extension.
An AFS extension is a dynamic linked library file that implements pre-defined interfaces. The interface header files are located at "sdk" sub-directory in AFS installed directory.
Request and Response
An AFS extension is a bridge between flash side and CPP side. The flash side and CPP side communicate asynchronously via a request-response way. The flash side sends request, and then CPP side responds. The CPP side may push data by continuous repsonses. The communication is finished by calling response.finish in CPP side. The typical flowchart of request-response is shown in the following picture.
If the the process of a request will take a long time in CPP side, it is recommended to do the action in another thread, or it may block UI action. When the action is finished, call response.finish in that thread to forward the asynchronous response to flash side.
Following is an example of AFS asynchronous extension. In this example, the flash side sends a request, and the CPP side processes the request when receiving it - A message box pops out, and the response will not return util the user clicks the OK button.
The CPP Part
Every AFS extension must export a function as the entrypoint.
extern "C" EXTENSION_API IExtensionFactory* GetExtensionFactory(void)
IExtensionFactory is one interface to create IExtension instance. Developers must implement IExtensionFactory and IExtension interface listed in sdk/IExtension.h.
class IExtensionFactory
{
public:
virtual IExtension* CreateExtension(const wchar_t* id) = 0;
};
class IExtension
{
public:
virtual ~IExtension() = 0 {};
virtual void onFlashRequest(IFlashRequest* request) = 0;
virtual void onExit() = 0;
};
Implementions of the method onFlashRequest should handle requests from flash side, and then send responses to flash side. The responses are sent asychronously.
class MyExtension : public IExtension
{
public:
virtual void onFlashRequest(IFlashRequest* request)
{
if (strEqual(request->getRequestName(), _T("TestExtension")))
{
HWND hwndParent = (HWND)_ttoi(request->parameters()->get(_T("parent")));
::MessageBox(hwndParent, _T("Click Ok to finish request"), _T(""), MB_OK);
request->response()->finish();
}
}
virtual void onExit()
{
delete this;
}
};
The Flash Part
The corresponding actionscript 3 code snippet in flash side:
var window:Window = Window.getWindow(this);
var request:Request = new Request("Extension");
request.requestName = "TestExtension";
request.parameters.parent = window.nativeHandle;
request.addEventListener(ResponseEvent.COMPLETE, onRequestComplete);
request.request();
test.text = "Sending Request...";
function onRequestComplete(e:ResponseEvent):void
{
test.text = "Response Received.";
}
Compile
The full source of this tutorial can be found at Cookbook/Extension directory in AFS installed directory. The CPP source should be compiled by VC++. The actionscript code is a mxml application, complile it with Flex 3 compiler, and don't forget to add AFS framework library to the build library path. AFS framework is located at sdk/framework/framework.swc in the installed directory of AFS.
So the example extension is finished. In the next part (link here), I will show you how to use the extension in AFS.
- asyncria's blog
- Login or register to post comments


Recent comments
8 weeks 2 hours ago
8 weeks 1 day ago
51 weeks 3 days ago