13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Communicating with other Flash Player and AIR instances<br />

package<br />

{<br />

import flash.net.LocalConnection;<br />

import flash.display.Sprite;<br />

public class ServerLC ext<strong>en</strong>ds Sprite<br />

{<br />

public function ServerLC()<br />

{<br />

var lc:LocalConnection = new LocalConnection();<br />

lc.cli<strong>en</strong>t = new CustomCli<strong>en</strong>t1();<br />

try<br />

{<br />

lc.connect("conn1");<br />

}<br />

catch (error:Error)<br />

{<br />

trace("error:: already connected");<br />

}<br />

}<br />

}<br />

}<br />

This code first creates a LocalConnection object named lc and sets the cli<strong>en</strong>t property to an object, cli<strong>en</strong>tObject.<br />

Wh<strong>en</strong> another application calls a method in this LocalConnection instance, the runtime looks for that method in the<br />

cli<strong>en</strong>tObject object.<br />

If you already have a connection with the specified name, an Argum<strong>en</strong>t Error exception is thrown, indicating that the<br />

connection attempt failed because the object is already connected.<br />

Wh<strong>en</strong>ever a Flash Player instance connects to this SWF file and tries to invoke any method for the specified local<br />

connection, the request is s<strong>en</strong>t to the class specified by the cli<strong>en</strong>t property, which is set to the CustomCli<strong>en</strong>t1 class:<br />

package<br />

{<br />

import flash.ev<strong>en</strong>ts.*;<br />

import flash.system.fscommand;<br />

import flash.utils.Timer;<br />

public class CustomCli<strong>en</strong>t1 ext<strong>en</strong>ds Object<br />

{<br />

public function doMessage(value:String = ""):void<br />

{<br />

trace(value);<br />

}<br />

public function doQuit():void<br />

{<br />

trace("quitting in 5 seconds");<br />

this.close();<br />

var quitTimer:Timer = new Timer(5000, 1);<br />

quitTimer.addEv<strong>en</strong>tList<strong>en</strong>er(TimerEv<strong>en</strong>t.TIMER, closeHandler);<br />

}<br />

public function closeHandler(ev<strong>en</strong>t:TimerEv<strong>en</strong>t):void<br />

{<br />

fscommand("quit");<br />

}<br />

}<br />

}<br />

Last updated 6/6/2012<br />

832

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!