How to use Diffusion wrapper
The HTML page that loads the Flash®/Silverlight® app, must to load the Diffusion™ wrapper script.
The following example shows the diffusion-wrapper.js file being included in a web page:
<html> <head> <script src="diffusion-wrapper.js" language="javascript"></script> </head> <body> ..... </body> </html>
The Flash/Silverlight app makes a call to the external DiffusionWrapper method setClientDetails, typically in the onConnection callback, supplying the client ID and server URL. In the example below, the method ExternalInterface.call("setClientDetails") provides DiffusionWrapper with the client ID and server URL. DiffusionWrapper adds a method to the onbeforeunload event of the window, which informs the Diffusion server that the client is intentionally closing the connection when the browser window or tab is closed.
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="371" height="334" minWidth="955" minHeight="600" layout="absolute"> <mx:Script> import com.pushtechnology.diffusion.ConnectionDetails; import com.pushtechnology.diffusion.DiffusionClient; import com.pushtechnology.diffusion.ServerDetails; import com.pushtechnology.diffusion.events.DiffusionConnectionEvent; import com.pushtechnology.diffusion.events.DiffusionExceptionEvent; import com.pushtechnology.diffusion.events.DiffusionMessageEvent; private var theServerUrl:String = "http://127.0.0.1:8080" private var theInitialTopic:String = "Echo" private var theClient:DiffusionClient; private function onConnect():void{ theClient = new DiffusionClient(); var theConnectionDetails:ConnectionDetails = new ConnectionDetails(new ServerDetails(theServerUrl),null); // Add the listeners theClient.addEventListener(DiffusionConnectionEvent.CONNECTION, onConnection); theClient.addEventListener(DiffusionMessageEvent.MESSAGE, onMessages); theClient.addEventListener(DiffusionExceptionEvent.EXCEPTION, onException); //Lets Go... theClient.connect(theConnectionDetails); } private function onConnection(event:DiffusionConnectionEvent):void{ // Is the client connected? if(event.isConnected()){ //Check the externalInterface availability if(ExternalInterface.available){ // Send the ClientId and serverURL to the DiffusionWrapper ExternalInterface.call("setClientDetails", theClient.getClientID(), event.getServerDetails().getURL()); }else{ //The externalInterface is not available, so the DiffusionWrapper is not called. } } } private function onMessages(event:DiffusionMessageEvent):void{ //Message received theMessages.text += event.toString(); } private function onException(event:DiffusionExceptionEvent):void{ //Exception received theMessages.text += event.toString(); } </mx:Script> <mx:TextArea id="theMessages" x="18" y="40" width="335" height="283" text="Messages"/> <mx:Button id="bConnect" x="105.5" y="10" width="160" label="Connect" click="onConnect()"/> </mx:Application>