Package | flash.net |
Class | public class LocalConnection |
Inheritance | LocalConnection ![]() ![]() |
Language Version: | ActionScript 3.0 |
Runtime Versions: | 1.0, 9 |
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.
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 SWF files, it is helpful to identify the commands used in each file.
One SWF file is called the
sending
SWF file; it is the file
that contains the method to be invoked. The sending SWF file must contain a LocalConnection
object and a call to the
send()
method.
The other SWF file is called the
receiving
SWF file;
it is the file that invokes the method. The receiving SWF file must contain another LocalConnection
object and a call to the
connect()
method.
Your use of
send()
and
connect()
differs depending on whether the
SWF 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 Flash Player allows
same-domain communication by default. When two SWF 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:
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:
Different domains with unpredictable domain names
.
Sometimes, you might want to make the SWF file with the receiving LocalConnection object
more portable between domains. To avoid specifying the domain name in the
send()
method,
but to indicate to Flash Player 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:
You can use LocalConnection objects to send and receive data within a single SWF 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.
See also
Property | Defined By | ||
---|---|---|---|
client : Object Indicates the object on which callback methods are invoked. | LocalConnection | ||
![]() | constructor : 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 SWF file. | LocalConnection | ||
![]() | prototype : Object [static] A reference to the prototype object of a class or function object. | Object |
Method | Defined By | ||
---|---|---|---|
Creates a LocalConnection object. | LocalConnection | ||
![]() | 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 | ||
Prepares a LocalConnection object to receive commands from a send() command
(called the sending LocalConnection object). | LocalConnection | ||
![]() | Dispatches an event into the event flow. | EventDispatcher | |
![]() | Checks whether the EventDispatcher object has any listeners registered for a specific type
of event. | EventDispatcher | |
![]() | Indicates whether an object has a specified property defined. | Object | |
![]() | Indicates whether an instance of the Object class is in the prototype chain of the object specified
as the parameter. | Object | |
![]() | Indicates whether the specified property exists and is enumerable. | Object | |
![]() | Removes a listener from the EventDispatcher object. | EventDispatcher | |
Invokes the method named methodName on a connection opened with the
connect(connectionName) method (the receiving LocalConnection
object). | LocalConnection | ||
![]() | Sets the availability of a dynamic property for loop operations. | Object | |
![]() | Returns the string representation of the specified object. | Object | |
![]() | Returns the primitive value of the specified object. | Object | |
![]() | Checks whether an event listener is registered with this EventDispatcher object or any of
its ancestors for the specified event type. | EventDispatcher |
Event | Summary | Defined By | ||
---|---|---|---|---|
![]() | [broadcast event] Dispatched when Flash Player gains operating system focus and becomes active. | EventDispatcher | ||
Dispatched when an exception is thrown asynchronously — that is, from native asynchronous code. | LocalConnection | |||
![]() | [broadcast event] Dispatched when Flash Player loses operating 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 |
client | property |
client:Object
[read-write] Language Version: | ActionScript 3.0 |
Runtime Versions: | 1.0, 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.
public function get client():Object
public function set client(value:Object):void
TypeError —
The
client
property must be set to a non-null object.
|
See also
domain | property |
domain:String
[read-only] Language Version: | ActionScript 3.0 |
Runtime Versions: | 1.0, 9 |
A string representing the domain of the location of the current SWF file.
In SWF files published for Flash Player 9 or later, the returned string is the exact domain of
the current SWF file, including subdomains. 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 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.
public function get domain():String
See also
LocalConnection | () | Constructor |
public function LocalConnection()
Language Version: | ActionScript 3.0 |
Runtime Versions: | 1.0, 9 |
Creates a LocalConnection object. You can use LocalConnection objects to enable communication between different SWF files that are running on the same client computer.
See also
allowDomain | () | method |
public function allowDomain(... domains):void
Language Version: | ActionScript 3.0 |
Runtime Versions: | 1.0, 9 |
Specifies one or more domains that can send LocalConnection calls to this LocalConnection instance.
You cannot 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
allowInsecureDomain()
method instead.
You may want to use this method so that a child SWF file from a different domain can make LocalConnection
calls to the parent SWF file, without knowing 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
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 SWF 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 SWF 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 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 the
loaderInfo.url
property in the
loaded SWF 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 SWF 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 SWF file that created it, and cross-domain calls to LocalConnection
objects are not allowed unless you call the
LocalConnection.allowDomain()
method in the
receiving SWF file.
For more information, see the following:
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:
|
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: | 1.0, 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 SWF files from HTTPS origins. This difference
is meaningful only if you call the
allowInsecureDomain()
method from a
SWF 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 SWF files to HTTPS SWF files, even within the same domain.
Calling
allowInsecureDomain()
is not recommended,
because it can compromise the security offered by HTTPS. When you
load a SWF file over HTTPS, you can be reasonably sure that that SWF file
will not be tampered with during delivery over the network. If you
then permit a non-HTTPS SWF file to make LocalConnection calls to the
HTTPS SWF file, you are accepting calls from a SWF 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 SWF file.
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, because 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 9 or later from HTTP files published for Flash Player 6 or earlier.
For more information related to security, see the following:
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:
|
ArgumentError — All parameters specified must be non-null strings. |
See also
close | () | method |
public function close():void
Language Version: | ActionScript 3.0 |
Runtime Versions: | 1.0, 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.
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: | 1.0, 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, Flash Player resolves
connectionName
into a value of
"
superdomain
:connectionName"
,
where
superdomain
is the superdomain of the SWF file that contains the
connect()
command. For example, if the SWF file that contains 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 that are 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
connect(connectionName)
method.
If you are implementing communication between SWF files in different domains, specifying a string
for
connectionName
that begins with an underscore (_) makes the SWF file with the
receiving LocalConnection object more portable between domains. Here are the two possible cases:
connectionName
does not begin with an underscore (_),
Flash Player adds a prefix 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 SWF 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.
connectionName
begins with an underscore (for example,
"_connectionName"
), Flash Player does not add a prefix 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 SWF 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 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 you call the
LocalConnection.allowDomain()
method in the
receiving SWF file. You can prevent a SWF 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.
For more information, see the following:
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.
|
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: | 1.0, 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, Flash Player 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:
connectionName
in both the sending and
receiving LocalConnection objects. In the SWF 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 SWF files in any domain.
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 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 you call the
LocalConnection.allowDomain()
method in the
receiving SWF file. You can prevent a SWF 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.
For more information, see the following:
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. |
securityError: — LocalConnection.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: — 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. |
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
asyncError | Event |
flash.events.AsyncErrorEvent
flash.events.AsyncErrorEvent.ASYNC_ERROR
Language Version: | ActionScript 3.0 |
Runtime Versions: | 1.0, 9 |
Dispatched when an exception is thrown asynchronously — that is, from native asynchronous code.
TheAsyncErrorEvent.ASYNC_ERROR
constant defines the value of the
type
property of an
asyncError
event object.
This event has the following properties:
Property | Value |
---|---|
bubbles
|
false
|
cancelable
|
false
; there is no default
behavior to cancel.
|
currentTarget
| The object that is actively processing the Event object with an event listener. |
target
| The object experiencing a network operation failure. |
error
| The error that triggered the event. |
securityError | Event |
flash.events.SecurityErrorEvent
flash.events.SecurityErrorEvent.SECURITY_ERROR
Language Version: | ActionScript 3.0 |
Runtime Versions: | 1.0, 9 |
Dispatched if a call to
LocalConnection.send()
attempts to send data to a different security sandbox.
SecurityErrorEvent.SECURITY_ERROR
constant defines the value of the
type
property of a
securityError
event object.
This event has the following properties:
Property | Value |
---|---|
bubbles
|
false
|
cancelable
|
false
; there is no default behavior to cancel.
|
currentTarget
| The object that is actively processing the Event object with an event listener. |
target
| The network object reporting the security error. |
text
| Text to be displayed as an error message. |
See also
status | Event |
flash.events.StatusEvent
flash.events.StatusEvent.STATUS
Language Version: | ActionScript 3.0 |
Runtime Versions: | 1.0, 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 SWF file refuses the connection, the call can fail
without notification to the sending SWF file.
type
property of a
status
event object.
This event has the following properties:
Property | Value |
---|---|
bubbles
|
false
|
cancelable
|
false
; there is no default behavior to cancel.
|
code
| A description of the object's status. |
currentTarget
| The object that is actively processing the Event object with an event listener. |
level
|
The category of the message, such as
"status"
,
"warning"
or
"error"
.
|
target
| The object reporting its status. |
See also
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); } } }
// 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); } } }