Adobe® Flex® 4 Language Reference
Hide Packages and Classes List |  Packages  |  Classes  |  Index  |  Appendixes
flash.net 
LocalConnection 
Packageflash.net
Classpublic class LocalConnection
InheritanceLocalConnection Inheritance EventDispatcher Inheritance Object

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0 Flash Player 9

The LocalConnection class lets you create a LocalConnection object that can invoke a method in another LocalConnection object. The communication can be:
  • Within a single SWF file
  • Between multiple SWF files
  • Between content (SWF-based or HTML-based) in AIR applications
  • Between content (SWF-based or HTML-based) in an AIR application and SWF content running in a browser

Local connections enable this kind of communication between SWF files without the use of fscommand() or JavaScript. LocalConnection objects can communicate only among files that are running on the same client computer, but they can be running in different applications — for example, a file running in a browser and a SWF file running in Adobe AIR.

LocalConnection objects created in ActionScript 3.0 can communicate with LocalConnection objects created in ActionScript 1.0 or 2.0. The reverse is also true: LocalConnection objects created in ActionScript 1.0 or 2.0 can communicate with LocalConnection objects created in ActionScript 3.0. Flash Player handles this communication between LocalConnection objects of different versions automatically.

To understand how to use LocalConnection objects to implement communication between two files, it is helpful to identify the commands used in each file. One file is called the sending file; it is the file that contains the method to be invoked. The sending file must contain a LocalConnection object and a call to the send() method. The other file is called the receiving file; it is the file that invokes the method. The receiving file must contain another LocalConnection object and a call to the connect() method.

Your use of send() and connect() differs depending on whether the files are in the same domain, in different domains with predictable domain names, or in different domains with unpredictable or dynamic domain names. The following paragraphs explain the three different situations, with code samples for each.

Same domain. This is the simplest way to use a LocalConnection object, to allow communication only between LocalConnection objects that are located in the same domain, because same-domain communication is permitted by default. When two files from the same domain communicate, you do not need to implement any special security measures, and you simply pass the same value for the connectionName parameter to both the connect() and send() methods:

Loading from the same domain

// receivingLC is in http://www.domain.com/receiving.swf
receivingLC.connect('myConnection');

// sendingLC is in http://www.domain.com/sending.swf
// myMethod() is defined in sending.swf
sendingLC.send('myConnection', 'myMethod');

Different domains with predictable domain names. When two SWF files from different domains communicate, you need to allow communication between the two domains by calling the allowDomain() method. You also need to qualify the connection name in the send() method with the receiving LocalConnection object's domain name:

Loading from separate domains

// receivingLC is in http://www.domain.com/receiving.swf
receivingLC.allowDomain('www.anotherdomain.com');
receivingLC.connect('myConnection');

// sendingLC is in http://www.anotherdomain.com/sending.swf
sendingLC.send('www.domain.com:myConnection', 'myMethod');

Different domains with unpredictable domain names. Sometimes, you might want to make the file with the receiving LocalConnection object more portable between domains. To avoid specifying the domain name in the send() method, but to indicate that the receiving and sending LocalConnection objects are not in the same domain, precede the connection name with an underscore (_), in both the connect() and send() calls. To allow communication between the two domains, call the allowDomain() method and pass the domains from which you want to allow LocalConnection calls. Alternatively, pass the wildcard (*) argument to allow calls from all domains:

Loading from unknown domain names

// receivingLC is in http://www.domain.com/receiving.swf
receivingLC.allowDomain('*');
receivingLC.connect('_myConnection');

// sendingLC is in http://www.anotherdomain.com/sending.swf
sendingLC.send('_myConnection', 'myMethod');

You can use LocalConnection objects to send and receive data within a single file, but this is not a typical implementation.

For more information about the send() and connect() methods, see the discussion of the connectionName parameter in the LocalConnection.send() and LocalConnection.connect()entries. Also, see the allowDomain() and domain entries.

View the examples

See also



Public Properties
 PropertyDefined By
  client : Object
Indicates the object on which callback methods are invoked.
LocalConnection
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
  domain : String
[read-only] A string representing the domain of the location of the current file.
LocalConnection
  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).
LocalConnection
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
Public Methods
 MethodDefined By
  
Creates a LocalConnection object.
LocalConnection
 Inherited
addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.
EventDispatcher
  
Specifies one or more domains that can send LocalConnection calls to this LocalConnection instance.
LocalConnection
  
Specifies one or more domains that can send LocalConnection calls to this LocalConnection object.
LocalConnection
  
Closes (disconnects) a LocalConnection object.
LocalConnection
  
connect(connectionName:String):void
Prepares a LocalConnection object to receive commands from a send() command (called the sending LocalConnection object).
LocalConnection
 Inherited
Dispatches an event into the event flow.
EventDispatcher
 Inherited
Checks whether the EventDispatcher object has any listeners registered for a specific type of event.
EventDispatcher
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
 Inherited
removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
Removes a listener from the EventDispatcher object.
EventDispatcher
  
send(connectionName:String, methodName:String, ... arguments):void
Invokes the method named methodName on a connection opened with the connect(connectionName) method (the receiving LocalConnection object).
LocalConnection
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
 Inherited
Returns the string representation of this object, formatted according to locale-specific conventions.
Object
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
 Inherited
Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.
EventDispatcher
Events
 Event Summary Defined By
 Inherited[broadcast event] Dispatched when the Flash Player or AIR application gains operating system focus and becomes active.EventDispatcher
  Dispatched when an exception is thrown asynchronously — that is, from native asynchronous code.LocalConnection
 Inherited[broadcast event] Dispatched when the Flash Player or AIR application operating loses system focus and is becoming inactive.EventDispatcher
  Dispatched if a call to LocalConnection.send() attempts to send data to a different security sandbox.LocalConnection
  Dispatched when a LocalConnection object reports its status.LocalConnection
Property Detail

client

property
client:Object

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0 Flash Player 9

Indicates the object on which callback methods are invoked. The default object is this, the local connection being created. You can set the client property to another object, and callback methods are invoked on that other object.



Implementation
    public function get client():Object
    public function set client(value:Object):void

Throws
TypeError — The client property must be set to a non-null object.

domain

property 
domain:String  [read-only]

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0 Flash Player 9

A string representing the domain of the location of the current file.

In content running in the application security sandbox in Adobe AIR (content installed with the AIR application), the runtime uses the string app# followed by the application ID for the AIR application (defined in the application descriptor file) in place of the superdomain. For example a connectionName for an application with the application ID com.example.air.MyApp connectionName resolves to "app#com.example.air.MyApp:connectionName".

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

If the current file is a local file residing on the client computer running in Flash Player, this command returns "localhost".

The most common ways to use this property are 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 to use it 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 property.



Implementation
    public function get domain():String

See also

isPerUser

property 
isPerUser:Boolean

Language Version: ActionScript 3.0
Runtime Versions: Flash Player 10.0.32, AIR 1.5.2

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, and in AIR 1.5.1 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 default value is false.



Implementation
    public function get isPerUser():Boolean
    public function set isPerUser(value:Boolean):void
Constructor Detail

LocalConnection

()Constructor
public function LocalConnection()

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0 Flash Player 9

Creates a LocalConnection object. You can use LocalConnection objects to enable communication between different files that are running on the same client computer.

See also

Method Detail

allowDomain

()method
public function allowDomain(... domains):void

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0 Flash Player 9

Specifies one or more domains that can send LocalConnection calls to this LocalConnection instance.

You cannot use this method to let files hosted using a secure protocol (HTTPS) allow access from files hosted in nonsecure protocols; you must use the allowInsecureDomain() method instead.

You may want to use this method so that a child file from a different domain can make LocalConnection calls to the parent file, without knowing the final domain from which the child 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 url property of the LoaderInfo object used with the load, to get the domain to use with the allowDomain() method. For example, if you use a Loader object to load a child file, once the file is loaded, you can check the contentLoaderInfo.url property of the Loader object, and parse the domain out of the full URL string. If you do this, make sure that you wait until the file is loaded, because the contentLoaderInfo.url property will not have its final, correct value until the file is completely loaded.

The opposite situation can also occur: you might create a child 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 the loaderInfo.url property in the loaded file. Again, you must parse the domain out of the full URL from loaderInfo.url. In this situation, you don't have to wait for the parent file to load; the parent will already be loaded by the time the child loads.

When using this method, consider the Flash Player security model. By default, a LocalConnection object is associated with the sandbox of the file that created it, and cross-domain calls to LocalConnection objects are not allowed unless you call the LocalConnection.allowDomain() method in the receiving file. However, in Adobe AIR, content in the application security sandbox (content installed with the AIR application) are not restricted by these security limitations.

For more information, see the following:

  • The security chapter in the Programming ActionScript 3.0 book and the latest comments on LiveDocs
  • The security chapter in the Programming ActionScript 3.0 book and the latest comments on LiveDocs
  • The Flash Player Developer Center Topic: Security

Note: The allowDomain() method has changed from the form it had in ActionScript 1.0 and 2.0. In those earlier versions, allowDomain was a callback method that you implemented. In ActionScript 3.0, allowDomain() is a built-in method of LocalConnection that you call. With this change, allowDomain() works in much the same way as flash.system.Security.allowDomain().

Parameters

... domains — One or more strings that name the domains from which you want to allow LocalConnection calls. This parameter has two special cases:
  • You can specify a wildcard character "*" to allow calls from all domains.
  • You can specify the string "localhost" to allow calls to this file from files that are installed locally. Flash Player 8 introduced security restrictions on local files. By default, a SWF file running in Flash Player that is allowed to access the Internet cannot also have access to the local file system. In Flash Player, if you specify "localhost", any local SWF file can access this SWF file.


Throws
ArgumentError — All parameters specified must be non-null strings.

See also

allowInsecureDomain

()method 
public function allowInsecureDomain(... domains):void

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0 Flash Player 9

Specifies one or more domains that can send LocalConnection calls to this LocalConnection object.

The allowInsecureDomain() method works just like the allowDomain() method, except that the allowInsecureDomain() method additionally permits SWF files from non-HTTPS origins to send LocalConnection calls to files from HTTPS origins. This difference is meaningful only if you call the allowInsecureDomain() method from a file that was loaded using HTTPS. You must call the allowInsecureDomain() method even if you are crossing a non-HTTPS/HTTPS boundary within the same domain; by default, LocalConnection calls are never permitted from non-HTTPS files to HTTPS files, even within the same domain.

Calling allowInsecureDomain() is not recommended, because it can compromise the security offered by HTTPS. When you load a file over HTTPS, you can be reasonably sure that the file will not be tampered with during delivery over the network. If you then permit a non-HTTPS file to make LocalConnection calls to the HTTPS file, you are accepting calls from a file that may in fact have been tampered with during delivery. This generally requires extra vigilance because you cannot trust the authenticity of LocalConnection calls arriving at your HTTPS file.

By default, files hosted using the HTTPS protocol can be accessed only by other 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, because it compromises HTTPS security. However, you might need to do so, for example, if you need to permit access to HTTPS SWF files published for Flash Player 9 or later from HTTP files SWF published for Flash Player 6 or earlier.

For more information related to security, see the following:

  • The security chapter in the Programming ActionScript 3.0 book and the latest comments on LiveDocs
  • The security chapter in the Programming ActionScript 3.0 book and the latest comments on LiveDocs
  • The Flash Player Developer Center Topic: Security

Parameters

... domains — One or more strings that name the domains from which you want to allow LocalConnection calls. There are two special cases for this parameter:
  • You can specify the wildcard character "*" to allow calls from all domains. Specifying "*" does not include local hosts.
  • You can specify the string "localhost" to allow calls to this SWF file from SWF files that are installed locally. Flash Player 8 introduced security restrictions on local SWF files. A SWF file that is allowed to access the Internet cannot also have access to the local file system. If you specify "localhost", any local SWF file can access this SWF file. Remember that you must also designate the calling SWF file as a local-with-networking SWF file at authoring time.


Throws
ArgumentError — All parameters specified must be non-null strings.

See also

close

()method 
public function close():void

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0 Flash Player 9

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 connect() command using the same connectionName parameter in another SWF file.


Throws
ArgumentError — The LocalConnection instance is not connected, so it cannot be closed.

See also

connect

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

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0 Flash Player 9

Prepares a LocalConnection object to receive commands from a 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.

To avoid a race condition, define the methods attached to the receiving LocalConnection object before calling this method, as shown in the LocalConnection class example.

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

In content running in the application security sandbox in Adobe AIR (content installed with the AIR application), the runtime uses the string app# followed by the application ID for the AIR application (defined in the application descriptor file) in place of the superdomain. For example a connectionName for an application with the application ID com.example.air.MyApp connectionName resolves to "app#com.example.air.MyApp:connectionName".

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 Player makes it simple for files that are located in the same domain to communicate with each other.

If you are implementing communication only between 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 connect(connectionName) method.

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

  • If the string for connectionNamedoes not begin with an underscore (_), a prefix is added with the superdomain and a colon (for example, "myDomain:connectionName"). Although this ensures that your connection does not conflict with connections of the same name from other domains, any sending LocalConnection objects must specify this superdomain (for example, "myDomain:connectionName"). If the file with the receiving LocalConnection object is moved to another domain, the player changes the prefix to reflect the new superdomain (for example, "anotherDomain:connectionName"). All sending LocalConnection objects would have to be manually edited to point to the new superdomain.
  • If the string for connectionNamebegins with an underscore (for example, "_connectionName"), a prefix is not added to the string. This means that the receiving and sending LocalConnection objects use identical strings for connectionName. If the receiving object uses allowDomain() to specify that connections from any domain will be accepted, the file with the receiving LocalConnection object can be moved to another domain without altering any sending LocalConnection objects.

For more information, see the discussion in the class overview and the discussion of connectionName in send(), and also the allowDomain() and 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.

When you use this method , consider the Flash Player security model. By default, a LocalConnection object is associated with the sandbox of the file that created it, and cross-domain calls to LocalConnection objects are not allowed unless you call the LocalConnection.allowDomain() method in the receiving file. You can prevent a file from using this method by setting the allowNetworking parameter of the the object and embed tags in the HTML page that contains the SWF content. However, in Adobe AIR, content in the application security sandbox (content installed with the AIR application) are not restricted by these security limitations.

For more information, see the following:

  • The security chapter in the Programming ActionScript 3.0 book and the latest comments on LiveDocs
  • The security chapter in the Programming ActionScript 3.0 book and the latest comments on LiveDocs
  • The Flash Player Developer Center Topic: Security

Parameters

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


Throws
TypeError — The value passed to the connectionName parameter must be non-null.
 
ArgumentError — This error can occur for three reasons: 1) The string value passed to the connectionName parameter was null. Pass a non-null value. 2) The value passed to the connectionName parameter contained a colon (:). Colons are used as special characters to separate the superdomain from the connectionName string in the send() method, not the connect()method. 3) The LocalConnection instance is already connected.

See also

send

()method 
public function send(connectionName:String, methodName:String, ... arguments):void

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0 Flash Player 9

Invokes the method named methodName on a connection opened with the connect(connectionName) method (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 send() throws an ArgumentError but your syntax is correct, try dividing the send() requests into multiple commands, each with less than 40K of data.

As discussed in the connect() entry, the current superdomain in added 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 the current superdomain is not added to connectionName. You can do this in one of the following two ways:

  • Use an underscore (_) at the beginning of connectionName in both the sending and receiving LocalConnection objects. In the file that contains the receiving object, use LocalConnection.allowDomain() to specify that connections from any domain will be accepted. This implementation lets you store your sending and receiving files in any domain.
  • Include the superdomain in connectionName in the sending LocalConnection object — for example, myDomain.com:myConnectionName. In the receiving object, use LocalConnection.allowDomain() to specify that connections from the specified superdomain will be accepted (in this case, myDomain.com) or that connections from any domain will be accepted.

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

When you use this method , consider the Flash Player security model. By default, a LocalConnection object is associated with the sandbox of the file that created it, and cross-domain calls to LocalConnection objects are not allowed unless you call the LocalConnection.allowDomain() method in the receiving file. For SWF content running in the browser, ou can prevent a file from using this method by setting the allowNetworking parameter of the the object and embed tags in the HTML page that contains the SWF content. However, in Adobe AIR, content in the application security sandbox (content installed with the AIR application) are not restricted by these security limitations.

For more information, see the following:

  • The security chapter in the Programming ActionScript 3.0 book and the latest comments on LiveDocs
  • The security chapter in the Programming ActionScript 3.0 book and the latest comments on LiveDocs
  • The Flash Player Developer Center Topic: Security

Parameters

connectionName:String — Corresponds to the connection name specified in the connect() command that wants to communicate with the sending LocalConnection object.
 
methodName:String — 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, allowDomain, allowInsecureDomain, client, and domain.
 
... arguments — Additional optional parameters to be passed to the specified method.


Events
securityError:SecurityErrorEventLocalConnection.send() attempted to communicate with a SWF file from a security sandbox to which the calling code does not have access. You can work around this in the receiver's implementation of LocalConnection.allowDomain().
 
status:StatusEvent — If the value of the level property is "status", the call was successful; if the value is "error", the call failed. The call can fail if the receiving SWF file refuses the connection.

Throws
TypeError — The value of either connectionName or methodName is null. Pass non-null values for these parameters.
 
ArgumentError — This error can occur for one of the following reasons: 1) The value of either connectionName or methodName is an empty string. Pass valid strings for these parameters. 2) The method specified in methodName is restricted. 3) The serialized message that is being sent is too large (larger than 40K).

See also

Event Detail

asyncError

Event
Event Object Type: flash.events.AsyncErrorEvent
property AsyncErrorEvent.type = flash.events.AsyncErrorEvent.ASYNC_ERROR

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0 Flash Player 9

Dispatched when an exception is thrown asynchronously — that is, from native asynchronous code.

The AsyncErrorEvent.ASYNC_ERROR constant defines the value of the type property of an asyncError event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
currentTargetThe object that is actively processing the Event object with an event listener.
targetThe object dispatching the event.
errorThe error that triggered the event.

securityError

Event  
Event Object Type: flash.events.SecurityErrorEvent
property SecurityErrorEvent.type = flash.events.SecurityErrorEvent.SECURITY_ERROR

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0 Flash Player 9

Dispatched if a call to LocalConnection.send() attempts to send data to a different security sandbox.

The SecurityErrorEvent.SECURITY_ERROR constant defines the value of the type property of a securityError event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
currentTargetThe object that is actively processing the Event object with an event listener.
targetThe network object reporting the security error.
textText to be displayed as an error message.

See also

status

Event  
Event Object Type: flash.events.StatusEvent
property StatusEvent.type = flash.events.StatusEvent.STATUS

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0 Flash Player 9

Dispatched when a LocalConnection object reports its status. If LocalConnection.send() is successful, the value of the status event object's level property is "status"; if the call fails, the level property is "error". If the receiving file refuses the connection, the call can fail without notification to the sending file.

Defines the value of the type property of a status event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
codeA description of the object's status.
currentTargetThe object that is actively processing the Event object with an event listener.
levelThe category of the message, such as "status", "warning" or "error".
targetThe object reporting its status.

See also

LocalConnectionSenderExample.as

This example consists of two ActionScript classes which should be compiled into two separate SWF files:

In the LocalConnectionSenderExample SWF file, a LocalConnection instance is created, and when the button is pressed the call() method is used to call the method named lcHandler in the SWF file with the connection name "myConnection," passing the contents of the TextField as a parameter.

In the LocalConnectionReceiverExample SWF file, a LocalConnection instance is created and the connect() method is called to designate this SWF file as the recipient of messages that are addressed to the connection named "myConnection." In addition, this class includes a public method named lcHandler(); this method is the one that is called by the LocalConnectionSenderExample SWF file. When it's called, the text that is passed in as a parameter is appended to the TextField on the Stage.

Note: To test the example, both SWF files must be loaded on the same computer simultaneously.


// Code in LocalConnectionSenderExample.as
package {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.net.LocalConnection;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.events.StatusEvent;
    import flash.text.TextFieldAutoSize;

    public class LocalConnectionSenderExample extends Sprite {
        private var conn:LocalConnection;
        
        // UI elements
        private var messageLabel:TextField;
        private var message:TextField;
        private var sendBtn:Sprite;
        
        public function LocalConnectionSenderExample() {
            buildUI();
            sendBtn.addEventListener(MouseEvent.CLICK, sendMessage);
            conn = new LocalConnection();
            conn.addEventListener(StatusEvent.STATUS, onStatus);
        }
        
        private function sendMessage(event:MouseEvent):void {
            conn.send("myConnection", "lcHandler", message.text);
        }
        
        private function onStatus(event:StatusEvent):void {
            switch (event.level) {
                case "status":
                    trace("LocalConnection.send() succeeded");
                    break;
                case "error":
                    trace("LocalConnection.send() failed");
                    break;
            }
        }
        
        private function buildUI():void {
            const hPadding:uint = 5;
            // messageLabel
            messageLabel = new TextField();
            messageLabel.x = 10;
            messageLabel.y = 10;
            messageLabel.text = "Text to send:";
            messageLabel.autoSize = TextFieldAutoSize.LEFT;
            addChild(messageLabel);
            
            // message
            message = new TextField();
            message.x = messageLabel.x + messageLabel.width + hPadding;
            message.y = 10;
            message.width = 120;
            message.height = 20;
            message.background = true;
            message.border = true;
            message.type = TextFieldType.INPUT;
            addChild(message);
            
            // sendBtn
            sendBtn = new Sprite();
            sendBtn.x = message.x + message.width + hPadding;
            sendBtn.y = 10;
            var sendLbl:TextField = new TextField();
            sendLbl.x = 1 + hPadding;
            sendLbl.y = 1;
            sendLbl.selectable = false;
            sendLbl.autoSize = TextFieldAutoSize.LEFT;
            sendLbl.text = "Send";
            sendBtn.addChild(sendLbl);
            sendBtn.graphics.lineStyle(1);
            sendBtn.graphics.beginFill(0xcccccc);
            sendBtn.graphics.drawRoundRect(0, 0, (sendLbl.width + 2 + hPadding + hPadding), (sendLbl.height + 2), 5, 5);
            sendBtn.graphics.endFill();
            addChild(sendBtn);
        }
    }
}
LocalConnectionReceiverExample.as

// Code in LocalConnectionReceiverExample.as
package {
    import flash.display.Sprite;
    import flash.net.LocalConnection;
    import flash.text.TextField;

    public class LocalConnectionReceiverExample extends Sprite {
        private var conn:LocalConnection;
        private var output:TextField;
        
        public function LocalConnectionReceiverExample()     {
            buildUI();
            
            conn = new LocalConnection();
            conn.client = this;
            try {
                conn.connect("myConnection");
            } catch (error:ArgumentError) {
                trace("Can't connect...the connection name is already being used by another SWF");
            }
        }
        
        public function lcHandler(msg:String):void {
            output.appendText(msg + "\n");
        }
        
        private function buildUI():void {
            output = new TextField();
            output.background = true;
            output.border = true;
            output.wordWrap = true;
            addChild(output);
        }
    }
}