PackageTop Level
Classpublic dynamic class LocalConnection
InheritanceLocalConnection Inheritance Object

Player version: Flash Player 6

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. (A projector is a SWF file saved in a format that can run as a stand-alone application--that is, without Flash Player.) 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.



Public Properties
 Property
  isPerUser : Boolean
Indicates whether the LocalConnection object is scoped to the current user (true) or is globally accessible to all users on the computer (false).
 Properties inherited from class Object
 __proto__, __resolve, constructor, prototype
Public Methods
 Method
  
Creates a LocalConnection object.
  
close():Void
Closes (disconnects) a LocalConnection object.
  
connect(connectionName:String):Boolean
Prepares a LocalConnection object to receive commands from a LocalConnection.send() command (called the sending LocalConnection object).
  
Returns a string representing the domain of the location of the current SWF file.
  
send(connectionName:String, methodName:String, [args:Object]):Boolean
Invokes the method named method on a connection opened with the LocalConnection.connect(connectionName) command (the receiving LocalConnection object).
 Methods inherited from class Object
 addProperty, hasOwnProperty, isPropertyEnumerable, isPrototypeOf, registerClass, toString, unwatch, valueOf, watch
Events
 EventSummaryDefined by
  
allowDomain = function([sendingDomain:String]) {}
Invoked whenever receiving_lc receives a request to invoke a method from a sending LocalConnection object.LocalConnection
  
allowInsecureDomain = function([sendingDomain:String]) {}
Invoked whenever receiving_lc, 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 nonsecure protocol.LocalConnection
  
onStatus = function(infoObject:Object) {}
Invoked after a sending LocalConnection object tries to send a command to a receiving LocalConnection object.LocalConnection
Property detail
isPerUserproperty
public var isPerUser:Boolean

Language version: ActionScript 2.0
Player version: Flash Player 10.0.32

Indicates whether the LocalConnection object is scoped to the current user (true) or is globally accessible to all users on the computer (false). This property only affects content running on Mac OS X; other platforms ignore this parameter. Connections on Windows and Linux operating systems are always per-user. In Flash Player 10.0.22 and earlier, all LocalConnection objects on Mac OS X have a global scope. Always set this property to true, unless you need to preserve compatibility with previous versions. In future releases, the default value of this property might change to true. The value of this property is locked for individual LocalConnection instances and cannot be altered after close() , send() or connect() methods are called on that instance.

The default value is false.

Constructor detail
LocalConnection()constructor
public function LocalConnection()

Player version: Flash Player 6

Creates a LocalConnection object.

See also


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);

Method detail
close()method
public function close():Void

Player version: Flash Player 6

Closes (disconnects) a LocalConnection object. Issue this command when you no longer want the object to accept commands—for example, when you want to issue a LocalConnection.connect() command using the same connectionName parameter in another SWF file.

See also


Example
The following example closes a connection called receiving_lc when you click a Button component instance called close_button:
this.createTextField("welcome_txt", this.getNextHighestDepth(), 10, 10, 100, 22);
this.createTextField("status_txt", this.getNextHighestDepth(), 10, 42, 100,44);

var receiving_lc:LocalConnection = new LocalConnection();
receiving_lc.sayHello = function(name:String) {
    welcome_txt.text = "Hello, "+name;
};
receiving_lc.connect("lc_name");
var closeListener:Object = new Object();
closeListener.click = function(evt:Object) {
    receiving_lc.close();
    status_txt.text = "connection closed";
};
close_button.addEventListener("click", closeListener);

The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components' DepthManager class instead of the MovieClip.getNextHighestDepth() method.

connect()method 
public function connect(connectionName:String):Boolean

Player version: Flash Player 6

Prepares a LocalConnection object to receive commands from a LocalConnection.send() command (called the sending LocalConnection object). The object used with this command is called the receiving LocalConnection object. The receiving and sending objects must be running on the same client computer.

Make sure you define the methods attached to receiving_lc before calling this method, as shown in all the examples in this section.

By default, Flash Player resolves connectionName into a value of "superdomain:connectionName", where superdomain is the superdomain of the SWF file containing the LocalConnection.connect() command. For example, if the SWF file containing the receiving LocalConnection object is located at www.someDomain.com, connectionName resolves to "someDomain.com:connectionName". (If a SWF file is located on the client computer, the value assigned to superdomain is "localhost".)

Also by default, Flash Player lets the receiving LocalConnection object accept commands only from sending LocalConnection objects whose connection name also resolves into a value of "superdomain:connectionName". In this way, Flash makes it simple for SWF files located in the same domain to communicate with each other.

If you are implementing communication only between SWF files in the same domain, specify a string for connectionName that does not begin with an underscore (_) and that does not specify a domain name (for example, "myDomain:connectionName"). Use the same string in the LocalConnection.connect(connectionName) command.

If you are implementing communication between SWF files in different domains, specifying a string for connectionName that begins with an underscore (_) will make the SWF with the receiving LocalConnection object more portable between domains. Here are the two possible cases:

For more information, see the discussion of connectionName in LocalConnection.send() and also the LocalConnection.allowDomain and LocalConnection.domain() entries.

Note: Colons are used as special characters to separate the superdomain from the connectionName string. A string for connectionName that contains a colon is not valid.

Parameters
connectionName:String — A string that corresponds to the connection name specified in the LocalConnection.send() command that wants to communicate with receiving_lc.

Returns
Boolean — A Boolean value: true if no other process running on the same client computer has already issued this command using the same value for the connectionName parameter; false otherwise.

See also


Example
The following example shows how a SWF file in a particular domain can invoke a method named printOut in a receiving SWF file in the same domain.

First, create one SWF file with the following code:

this.createTextField("tf", this.getNextHighestDepth(), 10, 10, 300, 100);
var aLocalConnection:LocalConnection = new LocalConnection();
aLocalConnection.connect("demoConnection");
aLocalConnection.printOut = function(aString:String):Void{
    tf.text += aString;
}

Then create a second with the following code:

var sending_lc:LocalConnection = new LocalConnection();
sending_lc.send("demoConnection", "printOut", "This is a message from file B. Hello.");

To test this example, run the first SWF file, and then run the second one.

Here is another example. SWF 1 contains the following code, which creates a new Sound object that plays back an MP3 file at runtime. A ProgressBar called playback_pb displays the playback progress of the MP3 file. A Label component instance called song_lbl displays the name of the MP3 file. Buttons in different SWF files will be used to control the playback using a LocalConnection object.

var playback_pb:mx.controls.ProgressBar;
var my_sound:Sound;
playback_pb.setStyle("themeColor", "haloBlue");
this.createEmptyMovieClip("timer_mc", this.getNextHighestDepth());
var receiving_lc:LocalConnection = new LocalConnection();
receiving_lc.playMP3 = function(mp3Path:String, mp3Name:String) {
    song_lbl.text = mp3Name;
    playback_pb.indeterminate = true;
    my_sound = new Sound();
    my_sound.onLoad = function(success:Boolean) {
    playback_pb.indeterminate = false;
    };
    my_sound.onSoundComplete = function() {
    delete timer_mc.onEnterFrame;
    };
    timer_mc.onEnterFrame = function() {
    playback_pb.setProgress(my_sound.position, my_sound.duration);
    };
    my_sound.loadSound(mp3Path, true);
};
receiving_lc.connect("lc_name");

SWF 2 contains a button called play_btn. When you click the button, it connects to SWF 1 and passes two variables. The first variable contains the MP3 file to stream, and the second variable is the filename that you display in the Label component instance in SWF 1.

play_btn.onRelease = function() {
    var sending_lc:LocalConnection = new LocalConnection();
    sending_lc.send("lc_name", "playMP3", "song1.mp3", "Album - 01 - Song");
};

SWF 3 contains a button called play_btn. When you click the button, it connects to SWF 1 and passes two variables. The first variable contains the MP3 file to stream, and the second variable is the filename that you display in the Label component instance in SWF 1.

play_btn.onRelease = function() {
    var sending_lc:LocalConnection = new LocalConnection();
    sending_lc.send("lc_name", "playMP3", "song2.mp3", "Album - 02 - Another Song");
};

The MovieClip.getNextHighestDepth() method used in these examples requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components' DepthManager class instead of the MovieClip.getNextHighestDepth() method.

domain()method 
public function domain():String

Player version: Flash Player 6 — Behavior changed in Flash Player 7.

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

In SWF files published for Flash Player 6, the returned string is the superdomain of the current SWF file. For example, if the SWF file is located at www.adobe.com, this command returns "adobe.com".

In SWF files published for Flash Player 7 or later, the returned string is the exact domain of the current SWF file. For example, if the SWF file is located at www.adobe.com, this command returns "www.adobe.com".

If the current SWF file is a local file residing on the client computer, this command returns "localhost".

The most common way to use this command is to include the domain name of the sending LocalConnection object as a parameter to the method you plan to invoke in the receiving LocalConnection object or with LocalConnection.allowDomain to accept commands from a specified domain. If you are enabling communication only between LocalConnection objects that are located in the same domain, you probably don't need to use this command.

Returns
String — A string representing the domain of the location of the current SWF file; for more information, see the Description section.

See also


Example
In the following example, a receiving SWF file accepts commands only from SWF files located in the same domain or at adobe.com:
// If both the sending and receiving SWF files are Flash Player 6,
// then use the superdomain
var my_lc:LocalConnection = new LocalConnection();
my_lc.allowDomain = function(sendingDomain):String{
    return (sendingDomain==this.domain() || sendingDomain=="adobe.com");
}

// If either the sending or receiving SWF file is Flash Player 7 or later, 
// then use the exact domain. In this case, commands from a SWF file posted
// at www.adobe.com will be accepted, but those from one posted at
// a different subdomain, e.g. blogs.adobe.com, will not.
var my_lc:LocalConnection = new LocalConnection();
my_lc.allowDomain = function(sendingDomain):String{
    return (sendingDomain==this.domain() || sendingDomain=="www.adobe.com");
}

In the following example, a sending SWF file located at www.yourdomain.com invokes a method in a receiving SWF file located at www.mydomain.com. The sending SWF file includes its domain name as a parameter to the method it invokes, so the receiving SWF file can return a reply value to a LocalConnection object in the correct domain. The sending SWF file also specifies that it will accept commands only from SWF files at mydomain.com.

Line numbers are included for reference purposes. The sequence of events is described in the following list:

        // determine our domain and see if we need to truncate it
    57    var channelDomain:String = lc.domain();
    58    if (getVersion() >= 7 && this.getSWFVersion() >= 7)
    59    {
            // split domain name into elements
    60      var domainArray:Array = channelDomain.split(".");
        
            // if more than two elements are found,
            // chop off first element to create superdomain
    61      if (domainArray.length > 2)
    62       {
    63         domainArray.shift();
    64         channelDomain = domainArray.join(".");
    65       }
    66    }
    
    67    lc.connect("result");
    68    lc.send("mydomain.com:sum", "aSum", channelDomain + ':' + "result",
"aResult", 123, 456);

send()method 
public function send(connectionName:String, methodName:String, [args:Object]):Boolean

Player version: Flash Player 6

Invokes the method named method on a connection opened with the LocalConnection.connect(connectionName) command (the receiving LocalConnection object). The object used with this command is called the sending LocalConnection object. The SWF files that contain the sending and receiving objects must be running on the same client computer.

There is a 40 kilobyte limit to the amount of data you can pass as parameters to this command. If the command returns false but your syntax is correct, try dividing the LocalConnection.send() requests into multiple commands, each with less than 40K of data.

As discussed in the entry LocalConnection.connect(), Flash adds the current superdomain to connectionName by default. If you are implementing communication between different domains, you need to define connectionName in both the sending and receiving LocalConnection objects in such a way that Flash does not add the current superdomain to connectionName. You can do this in one of the following two ways:

Note: You cannot specify a superdomain in connectionName in the receiving LocalConnection object--you can only do this in the sending LocalConnection object.

When using this method, consider the Flash Player security model. By default, a LocalConnection object is associated with the sandbox of the SWF file that created it, and cross-domain calls to LocalConnection objects are not allowed unless the LocalConnection.allowDomain() method has been invoked.

For more information related to security, see the following:

Parameters
connectionName:String — A string that corresponds to the connection name specified in the LocalConnection.connect() command that wants to communicate with sending_lc.
 
methodName:String — A string specifying the name of the method to be invoked in the receiving LocalConnection object. The following method names cause the command to fail: send, connect, close, domain, onStatus, and allowDomain.
 
args:Object [optional] — Arguments to be passed to the specified method.

Returns
Boolean — A Boolean value: true if Flash can carry out the request; false otherwise.

Note: A return value of true does not necessarily mean that Flash successfully connected to a receiving LocalConnection object; it means only that the command is syntactically correct. To determine whether the connection succeeded, see LocalConnection.onStatus.

See also


Example
For an example of communicating between LocalConnection objects located in the same domain, see LocalConnection.connect(). For an example of communicating between LocalConnection objects located in any domain, see LocalConnection.allowDomain. For an example of communicating between LocalConnection objects located in specified domains, see LocalConnection.allowDomain and LocalConnection.domain().

Event detail
allowDomainevent handler

public allowDomain = function([sendingDomain:String]) {}

Player version: Flash Player 7

Invoked whenever receiving_lc receives a request to invoke a method from a sending LocalConnection object. Flash expects the code you implement in this handler to return a Boolean value of true or false. If the handler doesn't return true, the request from the sending object is ignored, and the method is not invoked.

When this event handler is absent, Flash Player applies a default security policy, which is equivalent to the following code:

my_lc.allowDomain = function (sendingDomain)
{
    return (sendingDomain == this.domain());
}

Use LocalConnection.allowDomain to explicitly permit LocalConnection objects from specified domains, or from any domain, to execute methods of the receiving LocalConnection object. If you don't declare the sendingDomain parameter, you probably want to accept commands from any domain, and the code in your handler would be simply return true. If you do declare sendingDomain, you probably want to compare the value of sendingDomain with domains from which you want to accept commands. The following examples show both implementations.

In files authored for Flash Player 6, the sendingDomain parameter contains the superdomain of the caller. In files authored for Flash Player 7 or later, the sendingDomain parameter contains the exact domain of the caller. In the latter case, to allow access by SWF files hosted at either www.domain.com or store.domain.com, you must explicitly allow access from both domains.

// For Flash Player 6
receiving_lc.allowDomain = function(sendingDomain) {
    return(sendingDomain=="domain.com");
}
// For Flash Player 7 or later
receiving_lc.allowDomain = function(sendingDomain) {
    return(sendingDomain=="www.domain.com" ||
        sendingDomain=="store.domain.com");
}

Also, for files authored for Flash Player 7 or later, you can't use this method to let SWF files hosted using a secure protocol (HTTPS) allow access from SWF files hosted in nonsecure protocols; you must use the LocalConnection.allowInsecureDomain event handler instead.

Occasionally, you might encounter the following situation. Suppose you load a child SWF file from a different domain. You want to implement this method so that the child SWF file can make LocalConnection calls to the parent SWF file, but you don't know the final domain from which the child SWF file will come. This can happen, for example, when you use load-balancing redirects or third-party servers.

In this situation, you can use the MovieClip._url property in your implementation of this method. For example, if you load a SWF file into my_mc, you can then implement this method by checking whether the domain argument matches the domain of my_mc._url. (You must parse the domain out of the full URL contained in my_mc._url.)

If you do this, make sure that you wait until the SWF file in my_mc is loaded, because the _url property will not have its final, correct value until the file is completely loaded. The best way to determine when a child SWF file finishes loading is to use MovieClipLoader.onLoadComplete.

The opposite situation can also occur: You might create a child SWF file that wants to accept LocalConnection calls from its parent but doesn't know the domain of its parent. In this situation, implement this method by checking whether the domain argument matches the domain of _parent._url. Again, you must parse the domain out of the full URL from _parent._url. In this situation, you don't have to wait for the parent SWF file to load; the parent will already be loaded by the time the child loads.

Parameters
sendingDomain:String [optional] — A string that specifies the domain of the SWF file that contains the sending LocalConnection object.

Example
The following example shows how a LocalConnection object in a receiving SWF file can permit SWF files from any domain to invoke its methods. Compare this to the example in LocalConnection.connect(), in which only SWF files from the same domain can invoke the trace() method in the receiving SWF file. For a discussion of the use of the underscore (_) in the connection name, see LocalConnection.send().
this.createTextField("welcome_txt", this.getNextHighestDepth(), 10, 10, 100, 20);
var my_lc:LocalConnection = new LocalConnection();
my_lc.allowDomain = function(sendingDomain:String) {
    domain_txt.text = sendingDomain;
    return true;
};
my_lc.allowInsecureDomain = function(sendingDomain:String) {
    return (sendingDomain == this.domain());
}
my_lc.sayHello = function(name:String) {
    welcome_txt.text = "Hello, "+name;
};
my_lc.connect("_mylc");    

The following example sends a string to the previous SWF file and displays a status message about whether the local connection was able to connect to the file. A TextInput component called name_ti, a TextArea instance called status_ta and a Button instance called send_button are used to display content.

var sending_lc:LocalConnection;
var sendListener:Object = new Object();
sendListener.click = function(evt:Object) {
    sending_lc = new LocalConnection();
    sending_lc.onStatus = function(infoObject:Object) {
    switch (infoObject.level) {
    case 'status' :
        status_ta.text = "LocalConnection connected successfully.";
        break;
    case 'error' :
        status_ta.text = "LocalConnection encountered an error.";
        break;
    }
    };
    sending_lc.send("_mylc", "sayHello", name_ti.text);
};
send_button.addEventListener("click", sendListener);

If your SWF file includes a version 2 component, use the version 2 components' DepthManager class instead of the MovieClip.getNextHighestDepth() method used in the previous example.

In the following example, the receiving SWF file, which resides in thisDomain.com, accepts commands only from SWF files located in thisDomain.com or thatDomain.com:

var aLocalConn:LocalConnection = new LocalConnection();
aLocalConn.Trace = function(aString) {
    aTextField += aString+newline;
};
aLocalConn.allowDomain = function(sendingDomain) {
    return (sendingDomain == this.domain() || sendingDomain == "www.adobe.com");
};
aLocalConn.connect("_mylc");

When published for Flash Player 7 or later, exact domain matching is used. This means that the example will fail if the SWF files are located at www.thatDomain.com but will work if the files are located at thatDomain.com.

See also

allowInsecureDomainevent handler 

public allowInsecureDomain = function([sendingDomain:String]) {}

Player version: Flash Player 7

Invoked whenever receiving_lc, 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 nonsecure protocol. Flash expects the code you implement in this handler to return a Boolean value of true or false. If the handler doesn't return true, the request from the sending object is ignored, and the method is not invoked.

By default, SWF files hosted using the HTTPS protocol can be accessed only by other SWF files hosted using the HTTPS protocol. This implementation maintains the integrity provided by the HTTPS protocol.

Using this method to override the default behavior is not recommended, as it compromises HTTPS security. However, you might need to do so, for example, if you need to permit access to HTTPS files published for Flash Player 7 or later from HTTP files published for Flash Player 6.

A SWF file published for Flash Player 6 can use the LocalConnection.allowDomain event handler to permit HTTP to HTTPS access. However, because security is implemented differently in Flash Player 7, you must use the LocalConnection.allowInsecureDomain() method to permit such access in SWF files published for Flash Player 7 or later.

Parameters
sendingDomain:String [optional] — A string that specifies the domain of the SWF file that contains the sending LocalConnection object.

Example
The following example allows connections from the current domain or from www.adobe.com, or allows insecure connections only from the current domain.
this.createTextField("welcome_txt", this.getNextHighestDepth(), 10, 10, 100, 20);
var my_lc:LocalConnection = new LocalConnection();
my_lc.allowDomain = function(sendingDomain:String) {
    domain_txt.text = sendingDomain;
    return (sendingDomain == this.domain() || sendingDomain == "www.adobe.com");
};
my_lc.allowInsecureDomain = function(sendingDomain:String) {
    return (sendingDomain == this.domain());
}
my_lc.sayHello = function(name:String) {
    welcome_txt.text = "Hello, "+name;
};
my_lc.connect("lc_name");

If your SWF file includes a version 2 component, use the version 2 components' DepthManager class instead of the MovieClip.getNextHighestDepth() method used in this example.

See also

onStatusevent handler 

public onStatus = function(infoObject:Object) {}

Player version: Flash Player 6

Invoked after a sending LocalConnection object tries to send a command to a receiving LocalConnection object. If you want to respond to this event handler, you must create a function to process the information object sent by the LocalConnection object.

If the information object returned by this event handler contains a level value of status, Flash successfully sent the command to a receiving LocalConnection object. This does not mean that Flash successfully invoked the specified method of the receiving LocalConnection object; it means only that Flash could send the command. For example, the method is not invoked if the receiving LocalConnection object doesn't allow connections from the sending domain or if the method does not exist. The only way to know for sure if the method was invoked is to have the receiving object send a reply to the sending object.

If the information object returned by this event handler contains a level value of error, Flash cannot send the command to a receiving LocalConnection object, most likely because there is no receiving LocalConnection object connected whose name corresponds to the name specified in the sending_lc.send() command that invoked this handler.

In addition to this onStatus handler, Flash also provides a "super" function called System.onStatus. If onStatus is invoked for a particular object and there is no function assigned to respond to it, Flash processes a function assigned to System.onStatus if it exists.

In most cases, you implement this handler only to respond to error conditions, as shown in the following example.

Parameters
infoObject:Object — A parameter defined according to the status message. For details about this parameter, see the Description section.

Example
The following example displays a status message about whether the SWF file connects to another local connection object called lc_name. A TextInput component called name_ti, a TextArea instance called status_ta and a Button instance called send_button are used to display content.
var sending_lc:LocalConnection;
var sendListener:Object = new Object();
sendListener.click = function(evt:Object) {
    sending_lc = new LocalConnection();
    sending_lc.onStatus = function(infoObject:Object) {
        switch (infoObject.level) {
        case 'status' :
            status_ta.text = "LocalConnection connected successfully.";
                break;
        case 'error' :
            status_ta.text = "LocalConnection encountered an error.";
            break;
        }
    };
    sending_lc.send("lc_name", "sayHello", name_ti.text);
};
send_button.addEventListener("click", sendListener);

See also