« Spelling Plus Library | Main | Big Announcement Coming Soon! »
More fun with AIR LocalConnection (Source Included)
| By Rich Tretola | January 11, 2008 | Print This Post
|
| 1,180 views |
This sample has 2 components. The AIR portion and the Flex web portion. It allows you to drag and image into the AIR application and send it to the browser based Flex application via LocalConnection.
The AIR application accepting the image:

The image now showing in the browser:

Download and install this AIR application. Run the application, click the link to http://www.everythingflex.com/flex3/DropIt/Index.html to open the browser portion of the application.
As long as you have green check marks within both the AIR and Flex apps you are ready to give it a shot. Drag a small gif or jpg into the red box of the AIR application, wait a few seconds and you should get an Alert telling you to check your browser.
Here is the source (the AIR and Flex apps also have view source enabled)
AIR:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="absolute" creationComplete="init()"
- horizontalScrollPolicy="off" verticalScrollPolicy="off"
- width="425" height="325" backgroundColor="#FFFFFF" viewSourceURL="srcview/index.html">
- <mx:Script>
- <![CDATA[
- import mx.events.FlexEvent;
- import mx.graphics.codec.JPEGEncoder;
- import mx.controls.Alert;
- import flash.net.LocalConnection;
- import flash.events.StatusEvent;
- import mx.controls.Image;
- import flash.filesystem.File;
- [Bindable]
- private var targetURL:String = "http://www.everythingflex.com/flex3/DropIt/Index.html";
- public var outbound:LocalConnection = new LocalConnection();
- public var inbound:LocalConnection = new LocalConnection();
- [Bindable]
- private var isConnected:Boolean = true;
- [Bindable]
- [Embed(source="y.gif")]
- private var yes:Class;
- [Bindable]
- [Embed(source="x.gif")]
- private var no:Class;
- private var timer:Timer;
- private var bitmapData:BitmapData;
- public function init():void{
- isConnected = false;
- outbound.addEventListener(StatusEvent.STATUS, onStatus);
- inbound = new LocalConnection();
- inbound.allowDomain('*');
- inbound.client = this;
- try {
- inbound.connect("aircon");
- } catch (error:ArgumentError) {
- Alert.show("Connection Failure");
- }
- timer = new Timer(1000, 0);
- timer.addEventListener("timer", timerHandler);
- timer.start();
- this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,onDragIn);
- this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onDrop);
- }
- public function timerHandler(event:TimerEvent):void {
- outbound.send("everythingflex.com:flexcon", "testCon", outbound.domain);
- }
- public function testCon(s:String):void {
- isConnected = true;
- }
- public function getResponse(s:String):void {
- Alert.show(s,"Success");
- }
- private function onStatus(event:StatusEvent):void {
- if(event.level == "error"){
- isConnected = false;
- } else{
- isConnected = true;
- }
- }
- private function onDragIn(event:NativeDragEvent):void{
- NativeDragManager.acceptDragDrop(this);
- }
- private function onDrop(event:NativeDragEvent):void{
- var dropfiles:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
- for each (var file:File in dropfiles){
- switch (file.extension.toLowerCase()){
- case "png" :
- addImage(file.nativePath);
- break;
- case "jpg" :
- addImage(file.nativePath);
- break;
- case "jpeg" :
- addImage(file.nativePath);
- break;
- case "gif" :
- addImage(file.nativePath);
- break;
- default:
- Alert.show("Unmapped Extension");
- }
- }
- }
- private function addImage(nativePath:String):void{
- var i:Image = new Image();
- i.visible = false;
- i.addEventListener(FlexEvent.UPDATE_COMPLETE,sendJPEG);
- if(Capabilities.os.search("Mac") >= 0){
- i.source = "file://" + nativePath;
- } else {
- i.source = nativePath;
- }
- this.addChild(i);
- }
- private function sendJPEG(event:FlexEvent):void{
- try{
- var img:Image = event.target as Image;
- bitmapData = new BitmapData(img.width, img.height);
- bitmapData.draw(img);
- var bitmap:Bitmap = new Bitmap(bitmapData);
- var jpg:JPEGEncoder = new JPEGEncoder();
- var ba:ByteArray = jpg.encode(bitmapData);
- outbound.send("everythingflex.com:flexcon", "receiveImage", ba);
- } catch(e:Error){
- }
- }
- private function setVisuals(b:Boolean):Boolean{
- if(b){
- received.source = yes;
- message.text = "Drop a small image in the zone below";
- } else {
- received.source = no;
- message.text = "Make sure browser is open and pointing to:"
- }
- return b;
- }
- private function openBrowser():void{
- var url:String = targetURL;
- var urlRequest:URLRequest = new URLRequest(url);
- navigateToURL(urlRequest);
- }
- ]]>
- </mx:Script>
- <mx:Label text="Connected to Flex" x="146.5" y="282"/>
- <mx:Image id="received" x="260.5" y="282" width="16" height="18" source="{no}"/>
- <mx:Text id="message" x="10" y="10" width="403" textAlign="center" color="#000000" fontWeight="bold"/>
- <mx:LinkButton id="link" visible="{!setVisuals(isConnected)}" click="openBrowser()" label="{targetURL}" x="10" y="34" textAlign="center" width="100%"/>
- <mx:HBox id="dropzone" x="10" y="57" width="403" height="218" borderStyle="solid" borderThickness="3" borderColor="#E81414">
- </mx:HBox>
- </mx:WindowedApplication>
Flex:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
- creationComplete="init()" viewSourceURL="srcview/index.html">
- <mx:Script>
- <![CDATA[
- import flash.net.LocalConnection;
- import mx.controls.Alert;
- private var inbound:LocalConnection = new LocalConnection();;
- private var outbound:LocalConnection = new LocalConnection();;
- private var outboundDomain:String;
- private var timer:Timer;
- [Bindable]
- [Embed(source="y.gif")]
- private var yes:Class;
- [Bindable]
- [Embed(source="x.gif")]
- private var no:Class;
- public function init():void{
- outbound.addEventListener(StatusEvent.STATUS, onStatus);
- inbound.allowDomain('*');
- inbound.client = this;
- try {
- inbound.connect("flexcon");
- } catch (error:ArgumentError) {
- Alert.show("Connection failed, already in use", "Error");
- }
- }
- private function onStatus(event:StatusEvent):void {
- if(event.level == "error"){
- received.source = no;
- } else{
- received.source = yes;
- }
- }
- public function testCon(s:String):void{
- outboundDomain = s;
- outbound.send(outboundDomain + ":aircon", "testCon", "OK");
- timer = new Timer(1000, 0);
- timer.addEventListener("timer", timerHandler);
- timer.start();
- }
- public function timerHandler(event:TimerEvent):void {
- outbound.send(outboundDomain + ":aircon", "testCon", "OK");
- }
- public function receiveImage(ba:ByteArray):void {
- img.source = ba;
- respond();
- }
- private function respond():void {
- outbound.send(outboundDomain + ":aircon", "getResponse", 'Check you browser');
- }
- ]]>
- </mx:Script>
- <mx:Canvas horizontalCenter="0" verticalCenter="0" backgroundColor="#FFFFFF" width="100%" height="100%">
- <mx:Image id="img" x="10" y="36"/>
- <mx:Label text="Connected to AIR" x="241" y="14" color="#000000"/>
- <mx:Image id="received" x="352" y="14" width="16" height="16" source="{no}"/>
- </mx:Canvas>
- </mx:Application>
Topics: ActionScript 3, Adobe AIR, Flex 3 (Moxie) |








April 17th, 2008 at 2:22 pm
Hey i bought your book. And I have qeustions… How can I drag and drop any file, not just an image and encode it as a byteArray? I see in your example, you are using a flex event, is there another way to get this data from event target, for, such as .exe, .psd etc.
April 17th, 2008 at 2:51 pm
You can drag any file type into AIR, you would just need to add a new case statement to the switch in my example to handle the new file extension.
April 21st, 2008 at 7:06 am
Hi man, your AIR package is no longer supported by the new runtime, please recompile it!!
April 21st, 2008 at 3:48 pm
I just uploaded a newly compiled version.
April 26th, 2008 at 5:27 am
Ok many thanks, it works very well!!!!
Actually, I try but my published ID is not verified, so I have the felling that local connection works only if the AIR published ID is verifed?
Did you pay a certificate for this?
Many thanks
April 29th, 2008 at 3:49 pm
Rich, this is genius. What a great example of AIR! I’m curious, what led you to code this example?
April 29th, 2008 at 7:18 pm
Hey Chuckstar!
Just playing with AIR and thought why not.
I have another idea related to this sample that could actually have a business purpose.
October 23rd, 2008 at 9:58 am
Hi there,
I can get AS2 Flash in browser to communicate with AIR but not back again. ie I cannot get comms back from AIR to AS2 Flash in browser.
Any suggestions please.
October 24th, 2008 at 5:17 am
Still struggling with bi-directional AIR to AS2 LocalConnection. Any ideas out there?
November 13th, 2008 at 12:08 pm
is there a way to connect between 2 AIR applications on same pc?
is mandatory to have verified certificate to use localconnection ?
nice work u did