Log In  
 
  Getting Started  
----------------------------------------

Resources

 

Local Connections

  • A LocalConnection object is used to invoke a method in another LocalConnection object, either within a single SWF file or between multiple SWF files.
  • ActionScript 3.0 can communicate with LocalConnection objects created in ActionScript 1.0 or 2.0 and the reverse is true. Flash * Player handles this communication between LocalConnection objects of different versions automatically.
  • You can use LocalConnection objects to send and receive data within a single SWF file, but this is not a typical implementation
  • The sending SWF:
    • contains the method to be invoked
    • must contain a LocalConnection object
    • a call to the send() method passing the connectionName, methodName and arguments
  • The receiving SWF
    • invokes the method in the sending
    • must contain another LocalConnection object
    • a call to the connect() method passing the connectionName argument
  • Same domain connections are the easiest because Flash Player allows same-domain communication by default.
  • Use the allowDomain() method on the receiving SWF to allow communication between different domains with predictable domain names.
  • To avoid specifying the domain name in the send() method, but to indicate to Flash Player that the receiving and sending LocalConnection objects are not in the same domain, precede the connection name with an underscore (_), in both the connect() and send() calls.
previous page next page
Learn more

Example code: Two Flex 3 Applications Communicating over a LocalConnection

Below are two completely separate Flex applications, each runs in its own "space". They both use the LocalConnection class one sending a task to the other. The sending application uses the send() method of the LocalConnection class to send the task. It also registers an event handler function to receive status messages as to the success or failure of the send.
The Receiver application uses the connect() method of the LocalConnection class to "sign up" and then handles actual messages as they are sent.

TaskSender.MXML

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
	layout="absolute"
	creationComplete="initConn()"
	backgroundAlpha="0" backgroundColor="#FFFFFF">
	<mx:Script>
		<![CDATA[
		import flash.net.LocalConnection;

		private var conn:LocalConnection;

		private function initConn():void{
			btnSend.addEventListener(MouseEvent.CLICK, sendMessage);
			conn = new LocalConnection();
			conn.addEventListener(StatusEvent.STATUS, onStatus);
		}

		private function sendMessage(event:MouseEvent):void {
			conn.send("taskConnection", "localconnectionHandler", inputTask.text);
		}

		private function onStatus(event:StatusEvent):void {
			switch (event.level) {
				case "status":
					labelStatus.text = "LocalConnection.send() succeeded";
					break;
				case "error":
					labelStatus.text = "LocalConnection.send() failed";
					break;
			}
		}
		]]>
	</mx:Script>
	<mx:Panel horizontalCenter="0" verticalCenter="0">
		<mx:Form width="100%" height="100%" horizontalCenter="0" verticalCenter="0">
			<mx:FormItem label="Enter Task">
				<mx:TextInput id="inputTask"/>
			</mx:FormItem>
			<mx:FormItem label="Send Task ">
				<mx:Button id="btnSend" label="Send"/>
			</mx:FormItem>
			<mx:ControlBar>
				<mx:Label id="labelStatus" text=""/>
			</mx:ControlBar>
		</mx:Form>
	</mx:Panel>
</mx:Application>

BasicTaskReceiver.MXML

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
	layout="absolute"
	creationComplete="InitConn()"
	backgroundAlpha="0"
	backgroundColor="#FFFFFF">
	<mx:Script>
		<![CDATA[
			import flash.net.LocalConnection;

			private var conn:LocalConnection;

			private function InitConn():void{
				conn = new LocalConnection();
				conn.client = this;
				try {
					conn.connect("taskConnection");
				} catch (error:ArgumentError) {
					trace("Can't connect.");
				}
			}

			public function localconnectionHandler(msg:String):void {
				textareaTasks.text= textareaTasks.text + msg + "\n";
			}

			private function clearTasks(event:MouseEvent):void {
				textareaTasks.text="";
			}

		]]>
	</mx:Script>
	<mx:Panel  horizontalCenter="0"
		verticalCenter="0.5"
		verticalGap="15"
		paddingLeft="20" paddingRight="20" paddingBottom="20" paddingTop="20"
		height="300" width="500">
		<mx:Label text="Your tasks are..."/>
		<mx:TextArea id="textareaTasks"
			top="20" left="20" right="20" bottom="20"
			width="100%" height="100%"/>
		<mx:HBox>
			<mx:Button id="btnClearTasks" click="clearTasks(event)" label="Clear Tasks"/>
		</mx:HBox>
	</mx:Panel>
</mx:Application>

Rendered Example TaskSender

Rendered Example BasicTaskReceiver

previous page next page

What if I have two SWF's where one is loaded into the other via SWFLoader? Should I still use LocalConnection in that case, or there another better way to communicate between the two?

In this particular case, they both run in the system domain so that they don't try to use each other's imported WSDL reference and CSS.

Comment: Posted by Brent Lamborn at Mar 24, 2009 08:33

To answer my own question above, I found LocalConnection works fine with SWFLoader. I also found it is a good idea to call Close() in the reciever once the message is recieved, if the object that instatiates the LocalConnection is closed and opened more than once in an app, like say in a TitleWindow.

Comment: Posted by Brent Lamborn at Mar 24, 2009 12:47

I've been trying  localConnection, I actually copied your example.  They both through errors ( flex Builder3) the send application, Error 1061 (call to a possibly undefined  method send through a reference with static type LocalConnection ).

 On the receiving application, Error 1046 ( Type was not found or was not a compile- time constant:LocalConnection) , thats the first error here the second, 1180 (call to a possibly undefined method LocalConnection.  

HELP! HELP! HELP!

Comment: Posted by Keith Evanson at Dec 08, 2009 13:40
Added by Mark Nichoson, last edited by Randy Nielsen on Mar 21, 2010  (view change)

Labels:

Enter labels to add to this page:
Wait Image 
Looking for a label? Just start typing.