ActionScript 2.0 Language Reference |
|
|
|
| ActionScript classes > LocalConnection > connect (LocalConnection.connect method) | |||
public connect(connectionName:String) : Boolean
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:
"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 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"), Flash Player does not add a prefix to the string. This means that the receiving and sending LocalConnection objects will use identical strings for connectionName. If the receiving object uses LocalConnection.allowDomain to specify that connections from any domain will be accepted, the SWF with the receiving LocalConnection object can be moved to another domain without altering any sending LocalConnection objects.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.
Availability: ActionScript 1.0; Flash Player 6
connectionName:String - A string that corresponds to the connection name specified in the LocalConnection.send() command that wants to communicate with receiving_lc.
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.
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.
send (LocalConnection.send method), allowDomain (LocalConnection.allowDomain handler), domain (LocalConnection.domain method)
|
|
|
|