User Guide
382 Networking and Communication
The following code defines a local connection object that acts as a server and accepts
incoming calls from other Flash Player instances:
package
{
import flash.net.LocalConnection;
import flash.display.Sprite;
public class ServerLC extends Sprite
{
public function ServerLC()
{
var lc:LocalConnection = new LocalConnection();
lc.client = new CustomClient1();
try
{
lc.connect("conn1");
}
catch (error:Error)
{
trace("error:: already connected");
}
}
}
}
This code first creates a LocalConnection object named lc and sets the client property to a
custom class, CustomClient1. When another Flash Player instance calls a method in this local
connection instance, Flash Player looks for that method in the CustomClient1 class.
Whenever a Flash Player instance connects to this SWF file and tries to invoke any method
for the specified local connection, the request is sent to the class specified by the
client
property, which is set to the CustomClient1 class:
package
{
import flash.events.*;
import flash.system.fscommand;
import flash.utils.Timer;
public class CustomClient1 extends Object
{
public function doMessage(value:String = ""):void
{
trace(value);
}
public function doQuit():void
{
trace("quitting in 5 seconds");
this.close();
var quitTimer:Timer = new Timer(5000, 1);
quitTimer.addEventListener(TimerEvent.TIMER, closeHandler);