User Guide

Connecting to other Flash Player instances 383
}
public function closeHandler(event:TimerEvent):void
{
fscommand("quit");
}
}
}
To create a LocalConnection server, call the LocalConnection.connect() method and
provide a unique connection name. If you already have a connection with the specified name,
an
ArgumentError error is generated, indicating that the connection attempt failed because
the object is already connected.
The following snippet demonstrates how to create a new socket connection with the name
conn1:
try
{
connection.connect("conn1");
}
catch (error:ArgumentError)
{
trace("Error! Server already exists\n");
}
Connecting to the primary SWF file from a secondary SWF file requires that you create a new
LocalConnection object in the sending LocalConnection object and then call the
LocalConnection.send() method with the name of the connection and the name of the
method to execute. For example, to connect to the LocalConnection object that you created
earlier, you use the following code:
sendingConnection.send("conn1", "doQuit");
This code connects to an existing LocalConnection object with the connection name conn1
and invokes the
doQuit() method in the remote SWF file. If you want to send parameters to
the remote SWF file, you specify additional arguments after the method name in the
send()
method, as the following snippet shows:
sendingConnection.send("conn1", "doMessage", "Hello world");
NOTE
In earlier versions of ActionScript, the LocalConnection.connect() method returns a
Boolean value if the connection name has already been used. In ActionScript 3.0, an
error is generated if the name has already been used.