• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 504
  • Last Modified:

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.

0
ModifyMe
Asked:
ModifyMe
  • 3
  • 2
1 Solution
 
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
 
ModifyMeAuthor Commented:
Thanks you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now