Foreword
Ok, so I feeled like doing another tutorial, searched through the AS:Main and didn't find a tutorial about the LocalConnection class.
So, well, I made one:
What is the LocalConnection Class?
The LocalConnection class is used to send data from a .swf file to another .swf file without using any other complicated coding languages than ActionScript, no matter in what browsers or players the files are in.
All that is required is a .swf file that send information and one that listens to them.
Creating the Sending .swf
So, for the file that sends the information.
First you need to declare a variable for the class, like this:
var out:LocalConnection = new LocalConnection();
Ok, we got the class declared but at the moment this won't send any information at all.
To send information you use send(name:String, method:String, parameters);
Here is an example where the name of the connection is sName and the method is sMethod:
var out:LocalConnection = new LocalConnection();
out.send("sName", "sMethod");
To send parameters you will have to do like this:
var out:LocalConnection = new LocalConnection();
out.send("sName", "sMethod", 1, false, "three");
Creating the Receiving .swf
On with the creation of the file that will receive the parameters.
You have to declare a variable for the class in the receiving file aswell:
var inc:LocalConnection = new LocalConnection();
And now we have to create a function for the method that we sent the parameters with:
inc.methodName = function(params):Void {
trace("Method called");
};
Now to connect to the sender .swf we use connect(); with the connection name used in the sending swf:
inc.connect(connectionName);
So a example connecting from the send file example:
var inc:LocalConnection = new LocalConnection();
inc.sMethod = function(par1, par2, par3):Void {
trace(par1);
trace(par2);
trace(par3);
};
inc.connect(sName);
The receiving .swf continues to listen to connection when the connect(); method has been called, unless you close it, like this:
inc.close();
Sending and Receiving Across Other Websites
As default when you send and receive data through LocalConnection it is inside that domain.
Say you have the .swf on www.domain.com then it sends to the other movies that responds on that domain.
If you want to send across other domains you'll have to write the domain name before the connection name, separated by a semicolon:
out.send("domain.com:conName", methodName);
Never use the www. part here.
For the receiving part from other domains you'll have to set up which domains to allow.
This is done by allowDomain:
inc.allowDomain = function(domain:String){
return true;
}
That allows all domains.
Here's an example of a receiver that allows the domain www.domain.com and www.domain2.com:
inc.allowDomain = function(domain:String):Void {
return (domain == "domain.com" || domain == "domain2.com");
}
- GuyWithHisComp
