|
Page 4 of 5 How Merapi Works
Merapi is a framework that bridges an AIR application with a Java application, both running on the desktop. This communication is accomplished through a class that exists in Java and ActionScript called merapi.Bridge. The simplest way to interact from AIR to Java is by sending and receiving messages though the bridge. Sending a message from ActionScript:
var message : Message = new Message(); message.data = "Hello from Merapi Flex."; message.type = "Reply"; Bridge.instance.sendMessage( message ); Sending a message from Java:
Bridge bridge = Bridge.getInstance(); Message message = new Message(); message.setData("Hello from Merapi Java."); bridge.sendMessage(message); Receiving a message in Flex:
<merapi:BridgeInstance id="bridge" result="handleResult(event)" /> <mx:Script> <![CDATA[ private function handleResult( event : ResultEvent ) : void { var message : IMessage = event.result as IMessage;
Receiving a message in Java:Bridge.getInstance().registerMessageHandler("Reply", messageHandlerInstance ); public void handleMessage( IMessage message ) { System.out.println( message.getData() ); } More advanced features are available in the framework for creating proxy objects in ActionScript or Java that automatically cause a mirrored instantiations and invocations on the corresponding side of the bridge. This functionality is essentially an abstraction of the basic messaging framework described above.
|