View comments | RSS feed

LocalConnection class

Availability

Flash Player 6.

Description

The LocalConnection class lets you develop SWF files that can send instructions to each other without the use of fscommand() or JavaScript. LocalConnection objects can communicate only among SWF files that are running on the same client computer, but they can be running in different applications--for example, a SWF file running in a browser and a SWF file running in a projector. You can use LocalConnection objects to send and receive data within a single SWF file, but this is not a standard implementation; all the examples in this section illustrate communication between different SWF files.

The primary methods used to send and receive data are LocalConnection.send() and LocalConnection.connect(). At its most basic, your code will implement the following commands; notice that both the LocalConnection.send() and LocalConnection.connect() commands specify the same connection name, lc_name:

// Code in the receiving SWF file
this.createTextField("result_txt", 1, 10, 10, 100, 22);
result_txt.border = true;
var receiving_lc:LocalConnection = new LocalConnection();
receiving_lc.methodToExecute = function(param1:Number, param2:Number) {
   result_txt.text = param1+param2;
};
receiving_lc.connect("lc_name");

// Code in the sending SWF file
var sending_lc:LocalConnection = new LocalConnection();
sending_lc.send("lc_name", "methodToExecute", 5, 7);

The simplest way to use a LocalConnection object is to allow communication only between LocalConnection objects located in the same domain because you won't have security issues. However, if you need to allow communication between domains, you have several ways to implement security measures. For more information, see the discussion of the connectionName parameter in LocalConnection.send() and the LocalConnection.allowDomain and LocalConnection.domain() entries.

Method summary for the LocalConnection class

Method Description
LocalConnection.close()

Closes (disconnects) the LocalConnection object.

LocalConnection.connect()

Prepares the LocalConnection object to receive commands from a LocalConnection.send() command.

LocalConnection.domain()

Returns a string representing the superdomain of the location of the current SWF file.

LocalConnection.send()

Invokes a method on a specified LocalConnection object.

Event handler summary for the LocalConnection class

Event handler Description
LocalConnection.allowDomain

Invoked whenever the current (receiving) LocalConnection object receives a request to invoke a method from a sending LocalConnection object.

LocalConnection.allowInsecureDomain

Invoked whenever the current (receiving) LocalConnection object, which is in a SWF file hosted at a domain using a secure protocol (HTTPS), receives a request to invoke a method from a sending LocalConnection object that is in a SWF file hosted at a non-secure protocol.

LocalConnection.onStatus

Invoked after a sending LocalConnection object tries to send a command to a receiving LocalConnection object.

Constructor for the LocalConnection class

Availability

Flash Player 6.

Usage

new LocalConnection() : LocalConnection

Parameters

None.

Returns

A reference to a LocalConnection object.

Description

Constructor; creates a LocalConnection object.

Example

The following example shows how receiving and sending SWF files create LocalConnnection objects. The two SWF files can use the same name or different names for their respective LocalConnection objects. In this example they use different names.

// Code in the receiving SWF file
this.createTextField("result_txt", 1, 10, 10, 100, 22);
result_txt.border = true;
var receiving_lc:LocalConnection = new LocalConnection();
receiving_lc.methodToExecute = function(param1:Number, param2:Number) {
   result_txt.text = param1+param2;
};
receiving_lc.connect("lc_name");

The following SWF file sends the request to the first SWF file.

// Code in the sending SWF file
var sending_lc:LocalConnection = new LocalConnection();
sending_lc.send("lc_name", "methodToExecute", 5, 7);

See also

LocalConnection.connect(), LocalConnection.send()

Comments


recoveredfromflashMX2004 said on Aug 4, 2004 at 3:10 PM :
oscargoldman said on May 11, 2004 at 9:54 AM :

don't test movies in flash and then switch to an html page that has two .swf's sharing a connection. testing movies with establish the connection between the .fla test .swf and one on the html page, and the two embeded swf's won't talk to each other.


glophead said on Jun 14, 2004 at 11:56 AM :

I'm writing a flash app that uses a Tree component. The app is embedded
on an HTML page and receives its initial Tree data from a Web Server.

Multiple instances of the app may be invoked and I'd like to synchronize
the Tree across all of the instances. [e.g., Opened/closed nodes are
the same across all instances of the application].

Right now, I'm doing this via localConnection objects via a publish-subcribe model, but was wondering if there are other approaches that I should
consider. I'd prefer not to have all the .swf instances contact a web
server for the information as the server is bandwidth constrained.

Thanks much,

Z
dvhh said on Nov 10, 2004 at 2:45 PM :
Re to glophead :
using SharedObject is a great idea, it's compatible with most navigator/os combination ( as it is surely what you worry for )
but security and data size limit are the main issue for this implementation.

Of course I wonder how much data you can throw in a LocalConnection.
Paul Terhaar said on Feb 18, 2005 at 12:51 PM :
Is there a size limit on localConnection Objects, or the size of an array
in one? Is there a way to override it? I have a cd-rom project I'm
working on where I want to parse several xml files and store the
parsed data in a local connection object, where it is accessible to
several other swfs. I found out that after a certain amount of data, the
LocalConnection object simply fails to work. If I make the arrays
smaller, then it does work. Any ideas?
No screen name said on Apr 19, 2005 at 8:34 AM :
Hoi Paul,

There seems to be a limit to the data using the localconnection.

You can pass the data in a small "while" loop in chuncks of for example 250 bytes.

If any one knowd the actual size limit, please met me know.
No screen name said on Jun 7, 2005 at 9:26 AM :
LocalConnection fails to work across multiple displays in Unix:

I have written several apps and have had no problems with LocalConnection across different displays in Windows, since the multiple displays (1st and 2nd monitor) are treated as one extended workspace. Also, LocalConnection works fine when I run the communicating applications on one display from Solaris (Unix-based). However, if I try to have the application communicate across multiple displays (setenv $DISPLAY :0.1), LocalConnection no longer works.

I was wondering if this shortcoming has to do with the Mozilla browser I'm using and the environmental variables that are allocated to it at startup. Any help (especially from the developers) concerning this limitation when using LocalConnection across multiple displays in Unix would be greatly appreciated.

-Rob
nunomira said on Jul 27, 2005 at 4:16 PM :
Here's another example based on the one provided, but a bit more interesting.
When you write in the input text field of the sending SWF, you see exactly the same text appearing on the dynamic text field of the receiving SWF:

// Code in the receiving SWF file
// create the dynamic text field
this.createTextField("result_txt", 1, 10, 10, 100, 22);
result_txt.border = true;
// create the local connection in the receiving file
var receiving_lc:LocalConnection = new LocalConnection();
// define the function that is going to be triggered by the other movie (sending file)
receiving_lc.methodToExecute = function(t:String)
{
// write something in this text field
result_txt.text = t;
};
receiving_lc.connect("lc_name");


// Code in the sending SWF file
var sending_lc:LocalConnection = new LocalConnection();
/*
create an input text field
*/
this.createTextField("text_txt", 1, 10, 10, 100, 22);
text_txt.type = "input";
text_txt.border = true;
// when the text changes, i.e., when you write something
text_txt.onChanged = function()
{
// send the text to the other text field by calling the function "methodToExecute" and sending the content of the text field as a parameter
sending_lc.send("lc_name", "methodToExecute", this.text);
};
bobsmo said on Oct 6, 2005 at 9:09 AM :
Is there a problem with using this in Flash 8 ?
I can't get any of my localConnect swfs working when I publish them
in 8.

mySender_1 = new LocalConnection();
myReceiver_2 = new LocalConnection();
myReceiver_2.doAction_2 = function(doThat_2) {
if (doThat_2 == 1) {
stop();
} else if (doThat_2 == 2) {
gotoAndStop(1);

}
};
myReceiver_2.connect("myConnection_2");

onEnterFrame = function () {
_root.displayNumber = _root._currentframe;
};
stop();
xzone9 productions said on Sep 6, 2008 at 9:11 AM :
I haven't been able to locate a good example of two Flash SWF both using the send and receive in each. I assume you can only use the local connection one way, but the documentatiom doesn't state that. Even if that is so, I think that maybe you can build two connections and turn one off and the other on to switch from send to receive.
Can en example as above be displayed or provided?
Thank you.
xzone9 productions said on Sep 6, 2008 at 9:20 AM :
I have located a working example of both sending and receiving from each SWF.

http://blog.circlecube.com/2008/03/12/local-connection-actionscript-communicate-between-seperate-flash-files-tutorial/

Simple.
xzone9 productions said on Oct 19, 2008 at 4:43 PM :
Okay so now the problem arises where if th euser has inadvertently opened two browser windows, the second window will fail to transmit the local connection values. They are only being received by the first browser window that is open.
What is a good built-in method in adding a unique value to the connection to prevent this occurrence?

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001421.html