ActionScript
Implementing Two Ways LocalConnection In ActionScript 3
May 9, 2009
0

LocalConnection enables two or more Flash files to communicate with each other. You can also use other methods, such as SharedObject, but the benefit of LocalConnection is the real-time nature of LocalConnection. For more information, click Adobe documentation here.

For an introduction to LocalConnection, please see this article which explains how to set up a one-way LocalConnection between two swfs.

This advanced example shows two-ways connection — that is, two swfs communicates with each-other using LocalConnection.

Creating the LocalConnection

For two-ways communication, each swfs must create two LocalConnections. One for listening and the other one for sending, like illustrated below.

twowayslocalconnectiondiagramb

In this illustration, SWF A creates a LocalConnectionA which waits for messages, and SWF B creates a LocalConnectionB which waits for messages. When both swfs are running, there are two LocalConnections listening: LocalConnectionA, and LocalConnectionB.

If SWF A wants to send a message to SWF B,  it needs to create another LocalConnection, to target LocalConnectionB. Two LocalConnections are needed because the same instance cannot be used for sending and receiving (ie: e don’t want to interrupt LocalConnectionA which is listening for messages).   Note: you can theoretically re-use the LocalConnection, but it requires you to disconnect and re-connect.  It risks the possibility of interrupting the connection — what if something is sending to the connection while it’s disconnected?  There really is not much benefit to it.

Here’s the two swfs in action.  Move your mouse on either swf and the location of the ball should update in both places.  When you mouse over one of th swf, that swf  sends the coordinates of the ball to the other swf.

Both swfs uses a common class shown below:

package
{
	import flash.net.LocalConnection;
	import flash.events.StatusEvent;

	public class TwoWayLocalConnectionDemo
 	{
		private var mSenderLocalConnection:LocalConnection;
		private var mReceiverLocalConnection:LocalConnection;
		private var mOtherConnectioName:String;

		public function TwoWayLocalConnectionDemo(myConnectionName:String,
				otherConnectionName:String, listener:Object)
		{
			mSenderLocalConnection=new LocalConnection();
			mSenderLocalConnection.addEventListener(StatusEvent.STATUS,
				onSenderLocalConnectionStatus);
			mOtherConnectioName=otherConnectionName;

			mReceiverLocalConnection=new LocalConnection();
			mReceiverLocalConnection.connect(myConnectionName);
			mReceiverLocalConnection.client=listener;
			mReceiverLocalConnection.addEventListener(StatusEvent.STATUS,
				onReceiverLocalConnectionStatus);
		}

		public function send(newX:int, newY:int):void
		{
			mSenderLocalConnection.send(mOtherConnectioName, "messageHandler", newX, newY);
		}

		public function onReceiverLocalConnectionStatus(statusEvent:StatusEvent):void
		{
			if (statusEvent.level=="error")
			{
				trace("onReceiverLocalConnectionStatus "+statusEvent.code);
			}
		}

		public function onSenderLocalConnectionStatus(statusEvent:StatusEvent):void
		{
			if (statusEvent.level=="error")
			{
				trace("onSenderLocalConnectionStatus "+statusEvent.code);
			}
		}
	}
}


SWF A creates the class using TwoWayLocalConnectionDemo(“connectionA”, “connectionB”). This is telling the class to create two LocalConnections. otherConnectionName is the name of the LocalConnection it should “send messages to”. myConnectionName is the name of local connection that “listens to messages” from the other swf.

In SWF B, the parameter is reversed, that is: TwoWayLocalConnectionDemo(“connectionB”, “connectionA”).

This means that SWF A listens with a connection named connectionA; SWF B listens with a connection named connectionB.

The listener paramater is the object which will handle the messages. In our example, it is the Document class of the swf, which coule be any class that contains the function name being called. In our example, the function is named messageHandler, which takes two-parameters. The SWF A version can be seen below.


	public class ConnectionA extends MovieClip
 	{
		private var mTwoWayLocalConnection:TwoWayLocalConnectionDemo;

		public function ConnectionA()
		{
			mTwoWayLocalConnection=new TwoWayLocalConnectionDemo("ConnectionA",
				"ConnectionB", this);
			ballMovieClip.addEventListener(MouseEvent.MOUSE_MOVE, onBallMoved);
			ballMovieClip.startDrag(true);
		}

		public function onBallMoved(event:MouseEvent):void
		{
			mTwoWayLocalConnection.send(ballMovieClip.x, ballMovieClip.y);
		}

		public function messageHandler(newX:int, newY:int):void
  		{
			trace("Received message:n"+newX+" "+newY);
			ballMovieClip.x=newX;
			ballMovieClip.y=newY;
		}
	}


If SWF A wants to send a message to SWF B , it should target connectionB and if SWF B wants to send a message to SWF A, it should target connectionA

The FLA can be downloaded here.