Using LocalConnection in Actionscript 3.

Hey all,

I am very new to using LocalConnnection in Actionscript 3.  Can LocalConnection be a constant open connection between swf's?

Can I pass arguments from one swf to the other?  My swf1 has a function
var conn:LocalConnection;
conn = new LocalConnection();
conn.connect('connectionName');
conn.client = this;

function connFunc(arg1, arg2):void
{
	trace("arguments = " + arg1 +  "and " + arg2);
}

Open in new window

and I would like swf2 to call that function and pass arguments
var conn_btn:Button;
conn_btn.addEventListener(MouseEvent.CLICK, connMe);

function connMe(e:Event):void
{
	var conn:LocalConnection;
	conn = new LocalConnection();
	conn.send('connectionName', 'connFunc');	
	connFunc("hello", "world");//function in swf1
}

Open in new window

into the swf1 function.

...is this do-able?

Thank you.

ModifyMeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dgofmanCommented:
Yes you can, plus you can reused same class for server and client and implements multiple clients
(create many SWF files using my class)

private var _connectionInterval:Number;
private var _connectionName:String = "LocalConnection";

private function test():void{
	var lc1:LocalConnection = createConnection(0, onChanneOneResult);
	var lc2:LocalConnection = createConnection(1, onChanneOneResult);
	var lc3:LocalConnection = createConnection(2, onChanneOneResult);
	send(lc1, 1, "Send message to channel 1");
	send(lc1, 2, "Send message to channel 2");
	//Close channel 1
	lc1.close();
	//Open channel 1 again
	lc1 = createConnection(0, onChanneOneResult);
	send(lc2, 0, "Send message to channel 0");
	send(lc3, 0, "Send message to channel 0");
}

private function onChanneOneResult(channelId:String, params:String):void{
	trace(channelId + "-->" + params);
}

private function send(lc:LocalConnection, channel:Number, params:String):void{
	lc.client.$send(channel, params);
}

private function createConnection(channel:int, resultHandler:Function=null):LocalConnection{
	var lc:LocalConnection = new LocalConnection();
	lc.allowDomain("*");
	lc.client = {$result:resultHandler, $channel:channel};
	lc.client.$send = function(channel:int, params:String):void{
		lc.send(_connectionName + channel, "echo", lc.client.$channel, params);
	};
	lc.client.echo = function(channel:Number, params:String):void {
		lc.client.$result(channel, params);
	};
	if(resultHandler != null){ 
		try{
			clearInterval(_connectionInterval);
			lc.connect(_connectionName + channel);
		} catch (error:ArgumentError) {
			_connectionInterval = setInterval(createConnection, 500, channel, resultHandler);
		}
	}
	return lc;
}

Open in new window


0
ModifyMeAuthor Commented:
Cool.

Can you tell me how I access the function on the other swf?  Would I just prepend the function call with the other swf name?

SWF2:
swf1.connFunc("hey", "world");

Open in new window


I would like for the SWF's to do it when it's automatically launched (enterframe).
0
ModifyMeAuthor Commented:
Not sure why I asked about the above (last post), I know how to do that...long week.

With LocalConnection, will I have to make each swf (swf1 and swf2) both receivers and senders each?
0
dgofmanCommented:
You can create many classes and implement the same communication logic.
Just one requirement, you must use unique channel number or id.
Another option create only one class and except channel id from URL or pass as parameter.
In this case you can run unlimited SWF files communication via LocalConnection
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ModifyMeAuthor Commented:
Thanks you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.