asLocalConnect is a ActionScript 3 extension / replacement for the standard LocalConnection.
Its support a lot of more features and it is also interesting for beginners, because it is very easy to create and use a LocalConnction.
Please keep in Mind that English is not my native language, so I try my best to make a clear documentation in English.
When you find errors or something like that, please send me a short email what i can change or which part is mistakable.
Content
Manifestations about asLocalConnect
Errors, Improvements and Support
The normal LocalConnect have a lot of hurdles and obstacles, for e.g: when the flashfile is deliver from several server or a loadbalancer the localconnect don't worked in the internet explorer.
For this behavior there are some solution, but no solution which cover all needs and so asLocalConnect was originated.
The goal of this is to support the normal LocalConnection features and add some additional features to cover all the needs.
Difference between LocalConnection and asLocalConnect
Disadvantages of LocalConnection
- No Heartbeat support
- No Events, when the LocalConnection's are ready
- No Events, when a LocalConnection is lost
- No Broadcast function
- No Synchronization
- No helpfull Error-messages
- No support for multiple displaying of the same flashfile (e.g: different Browserwindows / Browsertabs)
- No Overview over functions which are handle over Localconnection
- Security-handling must be self coded
- Limitation for the amount of data which can be transferred of LocalConnection, but this limit is not documented :(
Advantages of asLocalConnect
- HeartBeat support for any number of LocalConnections
- Events for onLoad, onUnload and HeartBeat
- Broadcasting
- Simple Synchronization with a difference from -10ms/+10ms for several flashfiles
- Channel function, so is it possible to display the same flashfiles several times which out interfere each other.
- Overview over the functions which are handle over asLocalConnect
- Simple Securitylevel concept which covered most of the standard task
- Simple creation of LocalConnects and status monitoring for several LocalConnections
- It used timing and other tricks to avoid the data-limit / conenction-limit from LocalConnection
- more improvements are planed...
The License for asLocalConnect is the BSD License, the License for the examples / documentation is the Creative Commons (by-sa) License.
Please also check http://sourceforge.net/projects/aslocalconnect for more Information's.
asLocalConnect is hosted on SourceForge on this page you can download the lasted version of asLocalConnect and submit errors and improvements.
First of all there is no direct Support for this project, please understand I do this in my free time and it takes a lot of time to write such a documentation.
So I have no time or the resources to answer direct support requests. Support requests to the contact email of the web page will be ignore and not answered.
When you find Errors or have any idea of a improvement, please submit these over the Google Code web page http://code.google.com/p/asswfbit/.
asLocalConnect 0.5 is available as package, copy the corresponding package folder "de" to the same place as the FLA file.

This example shows a LocalConnection which asLocalConnection and Heartbeat support.
Define Receiver
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_FlashB:asLocalConnect = new asLocalConnect('FlashB');
lc_FlashB.setSecurity(5);
lc_FlashB.listen('test', function (_text:String) {
trace('Empfange: '+_text);
});
lc_FlashB.heartbeat('FlashA');
In this example the Name of the LocalConnection is "FlashB" and it will start a "trace" when something execute the function "test" over LocalConnection.
Its also start the heartbeat with the LocalConnection named "FlashA", so that "FlashA" know when "FlashB" is ready to receive commands.
The command setSecurity(5) lower the Security-level, so that other flashfile can access the LocalConnection for local tests.
Define Transmitter
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_FlashA:asLocalConnect = new asLocalConnect('FlashA');
lc_FlashA.onLoad(function () {
lc_FlashA.send('FlashB', 'test', 'Hello World');
});
lc_FlashA.heartbeat('FlashB');
This LocalConnection has the Name "FlashA" and when both flashfiles are ready it will send the string "Hello World" to "FlashB".
The last entry start the heartbeat with "FlashB" in the example before.
2 flashfiles which exchange data with asLocalConnect and Heartbeat
This example should show the different events which are possible with asLocalConnect.
The event-handler "onLoad" and "onUnload" make it possible to tell the flashfile which should done when the LocalConnection is complete or when it is lost.
Please open Example A and Example B in any order, this example can be also open in different browsers on the same computer.
When both Examples are open and the LocalConnection is ready there will be display a Input and a Textbox where you can transmit text over LocalConnection.
Immediately after the LocalConnection is lost, the Input and the Textbox will be hidden again.
ActionScript Code
The ActionScript code is only different in the LocalConnection name and the named to which LocalConnection the text should be send.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
show_ff(false);
status.text = "Loading...";
var lc_flasha:asLocalConnect = new asLocalConnect('FlashA');
lc_flasha.setSecurity(5);
lc_flasha.listen('rec_text', function (_text:String) {
if (_text) {
txt.text = txt.text+_text+"\n";
}
});
lc_flasha.onLoad(function () {
show_ff(true);
status.text = "Conneted";
});
lc_flasha.onUnload(function () {
show_ff(false);
status.text = "Disconnected";
});
lc_flasha.onHeartBeat(function () {
bumper.gotoAndPlay(1);
});
lc_flasha.heartbeat('FlashB');
Submit.addEventListener(MouseEvent.MOUSE_UP, function() {
if (text.text) {
lc_flasha.send('FlashB', 'rec_text', text.text);
text.text = "";
}
});
function show_ff(vis:Boolean) {
text.visible = vis;
txt.visible = vis;
Submit.visible = vis;
a.visible = vis;
b.visible = vis;
}
Instances and Objects:
asLocalConnect Name = FlashA
lc_flasha = asLocalConnect Object
text = TextInput
txt = TextArea
Submit = Button
a and b = Text above TextInput and TextArea
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
show_ff(false);
status.text = "Loading...";
var lc_flashb:asLocalConnect = new asLocalConnect('FlashB');
lc_flashb.setSecurity(5);
lc_flashb.listen('rec_text', function (_text:String) {
if (_text) {
txt.text = txt.text+_text+"\n";
}
});
lc_flashb.onLoad(function () {
show_ff(true);
status.text = "Connected";
});
lc_flashb.onUnload(function () {
show_ff(false);
status.text = "Disconnected";
});
lc_flashb.onHeartBeat(function () {
bumper.gotoAndPlay(1);
});
lc_flashb.heartbeat('FlashA');
Submit.addEventListener(MouseEvent.MOUSE_UP, function() {
if (text.text) {
lc_flashb.send('FlashA', 'rec_text', text.text);
text.text = "";
}
});
function show_ff(vis:Boolean) {
text.visible = vis;
txt.visible = vis;
Submit.visible = vis;
a.visible = vis;
b.visible = vis;
}
Instances and Objects:
asLocalConnect Name = FlashB
lc_flashb = asLocalConnect Object
text = TextInput
txt = TextArea
Submit = Button
a and b = Text above TextInput and TextArea
Synchronization with 9 flashfiles and broadcast function
In this example 9 flashfile will be loaded and try to make a LocalConnection between each flashfile.
There is a master flashfile which used the "broadcast" function and send the request to increase the counter to the other flashfiles.
The master flashfile is the green counter, the slaves flashfiles are the red counters.
ActionScript Code
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_master:asLocalConnect = new asLocalConnect('Master');
lc_master.setSecurity(5);
lc_master.verbose(5);
lc_master.onLoad(function () {
lc_master.broadcast('counter',txt_counter.text);
setInterval(counter, 500);
});
function counter() {
lc_master.broadcast('counter',++txt_counter.text);
}
lc_master.heartbeat('Master','Slave_1','Slave_2','Slave_3','Slave_4','Slave_5','Slave_6','Slave_7','Slave_8');
The master makes a heartbeat with all other slaves and when all are ready it will send the broadcast request.
When you open the same page in a additional browser window you will see that the flashfile don't interrupt them self.
This is the feature of the Channel function of asLocalConnect to avoid errors.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var Counter=0;
var lc_slave:asLocalConnect = new asLocalConnect('Slave_1');
lc_slave.setSecurity(5);
function setCounter(count:Number) {
if (count) {
txt_counter.text = count;
}
}
lc_slave.listen('counter',setCounter);
lc_slave.heartbeat('Master','Slave_1','Slave_2','Slave_3','Slave_4','Slave_5','Slave_6','Slave_7','Slave_8');
This is the ActionScript code for the first slave file, the other slaves has the same code except the asLocalConnect name and the heartbeat list.
Integration in existing projects or flashfiles
asLocalConnect is not compatible with LocalConnection, but it is simple to used some of the feature for existing projects or flashfiles without touch the existing code.
The most problems which LocalConnection occur because the timing of LocalConnection is not good enough to make a LocalConnection possible.
For this problem you can simple add an first empty frame and add in this frame asLocalConnect with the heartbeat function and when asLocalConnect found all LocalConnection, go to Frame 2 which the original code and the other normal LocalConnection.
You can also tell asLocalConnect when it lost the connection with the other flashfile, go back to frame 1 and wait.
The follow example so how you can make sure with asLocalConnect that both flashfiles are available and begin to started the animation.
Its important to make this in the flashfile which include the normal LocalConnection.
When you used parents and child's flashfiles, make sure to put this in the right parent and/or child's.
It has no sense to put this in the parent flashfile when only the child's make a LocalConnection.
ActionScript Code
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_flasha:asLocalConnect = new asLocalConnect('flasha');
lc_flasha.setSecurity(5);
lc_flasha.onLoad(function () {
mc_child.gotoAndPlay(2);
});
lc_flasha.heartbeat('flashb');
asLocalConnect make a heartbeat with "flashb", when both files a ready they will go to frame 2.
You can also use additional commands or different framenumbers / framenames.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_flashb:asLocalConnect = new asLocalConnect('flashb');
lc_flashb.setSecurity(5);
lc_flashb.onLoad(function () {
mc_child.gotoAndPlay(2);
});
lc_flashb.heartbeat('flasha');
This is not really different from the above example, its only used the LocalConnection name "flashb" and make the heartbeat with "flasha".
Please keep in mind that you should not used asLocalConnect as a preloader, because most of the feature are not supported at this way.
For e.g: with asLocalConnect you can open the same flashfiles 10 times, without that the flashfiles interrupt them self.
With the normal LocalConnection the flashfiles are interrupt them self so that only the first open flashfiles will worked.
Synchronization from several flashfiles accurate to milliseconds
asLocalConnect 0.5 and higher have the possible to synchronize several flashfiles with a different from -10ms/+10ms.
Its use several tricks and timings to archive this, also the synchronous execute from commands is possible.
ActionScript Code
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
// Init
mc_demo.stop();
btn_stop.visible = false;
btn_start.visible = false;
// Connection Stuff
var master=new asLocalConnect('master');
addChild(master);
master.setSecurity(5);
master.verbose(3);
master.onSync(function() {
txt_info.text = "Timer started at " + (new Date).getTime();
mc_demo.gotoAndPlay(1);
});
master.onLoad(function() {
btn_start.visible = true;
btn_stop.visible = true;
});
master.onUnload(function() {
btn_start.visible = false;
btn_stop.visible = false;
});
master.sync('master','slave1','slave2');
// Functions for Listenter
function syn_start() {
txt_info.text = "Syn Start recieved at " + (new Date).getTime();
mc_demo.gotoAndPlay(1);
}
function syn_stop() {
txt_info.text = "Syn Stop recieved at " + (new Date).getTime();
mc_demo.gotoAndStop(1);
}
master.listen("syn_start", syn_start);
master.listen("syn_stop", syn_stop);
// Button Event
btn_start.addEventListener(MouseEvent.MOUSE_UP, function() {
txt_info.text = "Please wait... send syn_start...";
master.sync_broadcast(true, "syn_start");
});
btn_stop.addEventListener(MouseEvent.MOUSE_UP, function() {
txt_info.text = "Please wait... send syn_stop...";
master.sync_broadcast(true, "syn_stop");
});
For the synchronization addChild(...) is very important. With the help of this command asLocalConnect get access to the Stage Object and can make more Optimization.
With the Events master.onLoad(...) and master.Unload(...) the flashfile know when it should make the buttons visible or hide them.
First of all with master.onSync you defined an event, which will be trigger synchronous when a synchronization was success.
Following with master.sync you start the synchronization, the first parameter is always the connection name of the Master, the rest parameters are the other flashfiles.
It is important to understand that there should be only one Master which control the timing for the synchronization.
To execute something you need to defined some Listener with master.listen(...) so that you can access them from the other flashfiles.
The synchronous broadcast will be execute with the command master.sync_broadcast(...), the first parameter define if the master should also execute the function or not.
In this case the Master should also execute the function, so this will be set to "true".
Instances and Objects:
asLocalConnect Name = master
master = asLocalConnect Object
mc_demo = Example MovieClip
btn_start = Button
btn_stop = Button
txt_info = Textfield for Infos
ActionScript Code
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
// Init
mc_demo.stop();
// Connection Stuff
var slave=new asLocalConnect('slave1');
addChild(slave);
slave.setSecurity(5);
slave.verbose(3);
slave.onSync(function() {
txt_info.text = "Timer started at " + (new Date).getTime();
mc_demo.gotoAndPlay(1);
});
slave.sync('master','slave1','slave2');
// Functions for Listenter
function syn_start() {
txt_info.text = "Syn Start recieved at " + (new Date).getTime();
mc_demo.gotoAndPlay(1);
}
function syn_stop() {
txt_info.text = "Syn Stop recieved at " + (new Date).getTime();
mc_demo.gotoAndStop(1);
}
slave.listen("syn_start", syn_start);
slave.listen("syn_stop", syn_stop);
Basically the ActionScript Code from the Slaves it not very different in compare to the Master.
Only the Buttons are missing and the connectionname is different.
Instances and Objects:
asLocalConnect Name = slave1
slave = asLocalConnect Object
mc_demo = Example MovieClip
txt_info = Textfield for Infos
ActionScript Code
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
// Init
mc_demo.stop();
// Connection Stuff
var slave=new asLocalConnect('slave2');
addChild(slave);
slave.setSecurity(5);
slave.verbose(3);
slave.onSync(function() {
txt_info.text = "Timer started at " + (new Date).getTime();
mc_demo.gotoAndPlay(1);
});
slave.sync('master','slave1','slave2');
// Functions for Listener
function syn_start() {
txt_info.text = "Syn Start recieved at " + (new Date).getTime();
mc_demo.gotoAndPlay(1);
}
function syn_stop(... args):void {
txt_info.text = "Syn Stop recieved at " + (new Date).getTime();
mc_demo.gotoAndStop(1);
}
slave.listen("syn_start", syn_start);
slave.listen("syn_stop", syn_stop);
Instanzname and Objectes:
asLocalConnect Name = slave2
slave = asLocalConnect Object
mc_demo = Example MovieClip
txt_info = Textfield for Infos
This is a detail overview, which show all functions, events and constructors of asLocalConnect.
Below this there are more details information for each function, event and constructor.
Overview over constructors
| Syntax | Description | ||
|---|---|---|---|
asLocalConnect(connectionName:String) | Create a asLocalConnect-Object. When there is a connectionName given, it will also connect to this name. | ||
Overview over functions
| Syntax | Description | ||
|---|---|---|---|
connect(connectionName:String) : Boolean | Prepare the LocalConnection-Object to recieve commands from the LocalConnection.send()-command (the sending LocalConnection-Object). | ||
listen(listnerName:String, functionName:Function) : void | Add a function to the LocalConnection Object , this function can be execute over the LocalConnection. | ||
allowDomain(domain:String) : void | Verifies if the domain is already in the allowDomain list, when not it will be add to this list. | ||
setSecurity(securityLevel:Number) : void | Set the security level for the LocalConnection, there a 8 different security levels. | ||
send(connectionName:String, methodName:String, args:Object) : void | Execute the method "method" for a connection which send a LocalConnection.connect( connectionName ) (the received LocalConnection-Object). | ||
broadcast(methodName:String, args:Object) : Boolean | Execute the method "method" for all open connections (the revieved LocalConnection-Objectes). | ||
sync_broadcast(selfExecute:String, methodName:String, args:Object) : void | The same as the function broadcast with the different that the command will be executed synchronously. | ||
domain() : String | Return the domain of the LocalConnection. | ||
sync(MasterConnectionName:String, connectionNames) : void | Start the synchronization for the selected flashfiles. | ||
heartbeat(connectionNames:Array) : void | Add a heartbeat for the passed connectionNames. | ||
heartbeatstatus(connectionName:String) : Boolean | Return the status for the passed connectionName. When no value is passed it return the status for all connectionNames. | ||
info() : void | Make a "trace" output with the LocalConnection Name, LocalConnection Channel and the Status. | ||
close() : void | Ended the LocalConnection and if need the Heartbeat. | ||
verbose(verboseLevel:Number) : void | Define the detail of the Information which will be display from asLocalConnect. | ||
Overview over Events
| Event | Description | ||
|---|---|---|---|
onLoad(onLoad_function:Function) : void | Is called, when all LocalConnections are ready. (HeartBeat) | ||
onUnload(onUnload_function:Function) : void | Is called, when the LocalConnection is lost. | ||
onSync(onSync_function:Function) : void | Will be trigger synchronously when all flashfiles have a synchronization. | ||
onHeartBeat(HeartBeat_function:Function) : void | Is called, for every send HeartBeat. | ||
onStatus(onStatus_function:Function) : void | Will be trigger when it is clear if the command .send(...) was success full or not. | ||
asLocalConnect(connectionName:String)
This will create the asLocalConnect object and assign it to a variable, this is needed to access this object later.
You can also pass the ConnectionName, when this is passed, the asLocalConnect object will automatic connect to this ConnectionName so there is not need to used connect(...).
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_Flash123:asLocalConnect = new asLocalConnect('Flash123');
In this example the LocalConnection Object with the Name "lc_Flash123" will created with the ConnectionName "Flash123".
Please attend that the ConnectionName need to be 4 characters and more, the ConnectionName will automatic add internal a "_" before the ConnectionName, so you don't need to pass the name with a "_".
But when you pass the named with a "_" e.g: "_Flash123", asLocalConnect detect this and can handel this as well.
connect(connectionName:String) : Boolean
With connect you set the connectionName for this connection, when you already pass this connectionName, you don't need the connect command.
When you don't pass the connectionName, you can use connect to set up a connectionName for this connection.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_Flash123:asLocalConnect = new asLocalConnect();
lc_Flash123.connect('Flash123');
This example is in the end the same as the above example, but the different is that here the object is first declare and after this the connection with the connectionName will be made.
listen(listnerName:String, functionName:Function) : void
"listen" defined which listnerName will execute which functionName, this function will execute when a asLocalConnect Object send a request for the listnerName.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_Flash123:asLocalConnect = new asLocalConnect('Flash123');
// Beispiel 1
lc_Flash123.listen('hello',function () {
trace('Hello World');
});
// Beispiel 2
function helloWorld() {
trace('Hello World');
}
lc_Flash123.listen('world',helloWorld);
In this example trace('Hello World'); will be executed when a asLocalConnect Object send a request to Flash123 with the name hello or world.
allowDomain(domain:String) : void
With "allowDomain" you defined which domains could communicate with the asLocalConnect object, for this is also relevate to used the right setSecurity option.
You can only use wild-chars, when the setSecurity is set to 6 or lower.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_Flash123:asLocalConnect = new asLocalConnect('Flash123');
lc_Flash123.setSecurity(6);
lc_Flash123.allowDomain('reifanhp.de');
lc_Flash123.allowDomain('animearchive.de');
In this example is the domain reifanhp.de and animearchive.de add to the AllowDomain list, flashfiles from this domain are allow to communicate with the flashfile in this example.
setSecurity(securityLevel:Number) : void
In asLocalConnect there is a security model, which responds depends on the security level.
Because of the heave changes in ActionScript 3 it is not possible to provide the same options as in earlier versions of asLocalConnect.
| Security Level | Describtion | ||
|---|---|---|---|
| 1 | No Security, allow all Domains, please only used for testing ! | ||
| 2 | Not in used. Dont used ! | ||
| 3 | Not in used. Dont used ! | ||
| 4 | Not in used. Dont used ! | ||
| 5 | The same as 6 with the different it will automatic allow local testing, so it is the best when you need to check the flashfiles local on your pc. | ||
| 6 | (Default) Allow all domains from AllowDomain and the same domain in which are the flashfile. | ||
| 7 | Allow only domains from AllowDomain and ignore same domain when this is not include in the AllowDomain list ! | ||
| 8 | Allow only the same domain and ignore the AllowDomain. | ||
| 9 | Offline Mode, no connections are allowed | ||
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_Flash123:asLocalConnect = new asLocalConnect('Flash123');
lc_Flash123.setSecurity(7);
lc_Flash123.allowDomain('reifanhp.de');
lc_Flash123.allowDomain('animearchive.de');
In this example flashfiles from the domain reifanhp.de and animearchive.de are allow to make a connection to this flashfile.
But connection from the same domain e.g: flash.area-network.de are not allowed and will be ignore.
send(connectionName:String, methodName:String, args:Object) : void
Send data to the pass connectionName and methodName with the given args (arguments), the methodName is defined over the function listen.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_Flash123:asLocalConnect = new asLocalConnect('Flash123');
lc_Flash321.send('Flash123','hello','evtl. Parameter');
In this example a request to Flash123 to execute the method hello, this was defined in the listen example as a trace output with "Hello World".
broadcast(methodName:String, args:Object) : Boolean
Send data to all over heartbeat defined connections with the pass methodName and args (arguments), the methodName is defined over the function listen.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_master:asLocalConnect = new asLocalConnect('Master');
lc_master.onLoad(function () {
lc_master.broadcast('counter', Counter);
});
lc_master.heartbeat('slave_1','slave_2','slave_3','slave_4','slave_5','slave_6','slave_7','slave_8');
This example is explained in "Exented examples".
sync_broadcast(methodName:String, args:Object) : void
Send data to all connection names which are accessible over sync and to the specific method which was defined over listen.
The different is that this will be happen synchronously, for each flashfile which are synchronize with sync.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_master:asLocalConnect = new asLocalConnect('Master');
addChild(lc_master);
lc_master.onSync(function () {
lc_master.sync_broadcast('counter', Counter);
});
lc_master.sync('Master','Slave1','Slave2');
The normal Broadcast "broadcast(...)" can be also used for LocalConnection which are synchronize with sync.
domain() : String
Return the domain as String.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_Flash123:asLocalConnect = new asLocalConnect('Flash123');
trace('Local Connection Domain: '+ lc_Flash321.domain());
This example will return the Domain from the LocalConnection over a trace.
heartbeat(connectionNames:Array) : void
Add a heartbeat for the passed connectionNames, you should always used the heartbeat to make sure that all LocalConnections are ready.
The heartbeat use special timing to make this processor friendly, to that the heartbeat has no big impact on the CPU load.
In former version it was needed not to include the own connection name, this is not needed in this version anymore.
asLocalConnect know his own connection name so it will detected if it was passed to the command or not.
When you have 3 or more connections, you should passed all connection names for any better overview.
A lot of method e.g: broadcast need the HeartBeat to worked correct.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_master:asLocalConnect = new asLocalConnect('Master');
lc_master.onLoad(function () {
lc_master.broadcast('counter', Counter);
});
lc_master.heartbeat('slave_1','slave_2','slave_3','slave_4','slave_5','slave_6','slave_7','slave_8');
In this example a HeartBeat with slave_1, slave_2, slave_3, slave_4, slave_5, slave_6 , slave_7, slave_8 is started, so the Master knows when all are ready or if someone lost the connection
Please keep in mind that every asLocalConnect Object should used a Heartbeat to the others asLocalConnect Objects, which are exchange data with this asLocalConnect Object.
sync(MasterConnectionName:String, connectionNames) : void
Start the synchronization for the passed connectionNames, it will also try to adjust the framerates of the flashfiles to that all frame are in the same timeframe.
For this reason you should make sure that there is no animation or other frame depended things before the synchronization is complete.
Animations should be only started after the synchronization was complete, which take only 2-3 seconds.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_master:asLocalConnect = new asLocalConnect('Master');
addChild(lc_master);
lc_master.onSync(function() {
txt_info.text = "Timer started at " + (new Date).getTime();
mc_demo.gotoAndPlay(1);
});
lc_master.sync('Master','Child1','Child2');
The command addChild(lc_master) is needed for the optimization of the framerate, when this command will not be used these important optimization will be skipped.
With the first parameter you specific the Master Connection Name, the Master Connection Name need to be the same for all Connections.
The rest of the parameter are the other Connection Name which are needed for the Connection.
Basically the own Connection Name, can be left (only when not master) because asLocalConnect check automatic if the own Connection Name was passed or not.
But for an better overview, you should include the own Connection Name, too.
heartbeatstatus(connectionName:String) : Boolean
Return the current status for the passed connectionName. When no connectionName is passed, the status for all connectionNames will returned.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_master:asLocalConnect = new asLocalConnect('Master');
lc_master.heartbeat('slave_1','slave_2','slave_3','slave_4','slave_5','slave_6','slave_7','slave_8');
// Status von 'slave_1' abfragen
lc_master.heartbeatstatus('slave_1');
// Allgemeinen Status alle Verbindungen abfragen
lc_master.heartbeatstatus();
In this example the status will be return as trace and as return value.
info() : void
This start a "trace" output with the LocalConnection Name, LocalConnection Channel and the Status of this Connection. The status means if this Object is connect to a LocalConnection Name.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_master:asLocalConnect = new asLocalConnect('Master');
lc_master.info();
The channel is used to make sure that several flashfile with the same connectionName don't interrupt them self.
close() : void
Ended the LocalConnection and if need the Heartbeat.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_master:asLocalConnect = new asLocalConnect('Master');
// Status before close()
lc_master.info();
// Close Connection
trace('- - -');
lc_master.close();
// Status after close()
lc_master.info();
In this example the connection with the name Master was ended.
verbose() : void
The Verbose Level specify how many Information you want to see from asLocalConnect.
These Information will be send over trace, so you can check them with the Flash Editor or with the Flash Debugplayer.
When you exclude trace() options in your export to the SWF file, then nothing will be displayed.
| Verbose Level | Description | ||
|---|---|---|---|
| 0 | ERROR: Show all fatal Errors. | ||
| 1 | WARNING: Show warning and try to point out possible errors. | ||
| 2 | INFO: Show additional Information's for e.g: what asLocalConnect has receive. | ||
| 3 | DEBUG: Show additional Information for e.g: what asLocalConnect is doing and why. | ||
| 4 | -: Not used yet. | ||
| 5 | ALL: Show all Messages from asLocalConnect. | ||
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_master:asLocalConnect = new asLocalConnect('Master');
lc_master.verbose(6);
In this example the verbose level is set to 6.
The events are the essential part of asLocalConnect, with the help of the events is it possible to control the flashfiles. How they should act when all asLocalConnection objects are ready or when a connection is lost.
onLoad(onLoad_function:Function) : void
This event will be called when all connection defined over heartbeat are ready and available
.
You should used this to execute commands when all objects are ready, e.g: start a animation .
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_flasha:asLocalConnect = new asLocalConnect('FlashA');
lc_flasha.onLoad(function () {
show_ff(true);
lc_flasha.send('FlashB', 'rec_text', "Hello World");
status = "Connected";
});
lc_flasha.heartbeat('FlashB');
When FlashB is ready, FlashA will execute the function show_ff(true) and send "Hello World" to FlashB, its also set the variable status to "Connected".
onUnload(onUnload_function:Function) : void
This event will be called when a connection is lost after a successful connection.
So you can very easy detect when the other flashfile was closed.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_flasha:asLocalConnect = new asLocalConnect('FlashA');
lc_flasha.onUnload(function () {
show_ff(false);
status = "Disconnected";
});
lc_flasha.heartbeat('FlashB');
If FlashA lost the connection to FlashB, the function show_ff(false) will be executed and the variable status will set to "Disconnected".
onSync(onSync_function:Function) : void
This Event will trigger synchronous when all flashfiles are synchronized.
Per connection there should only a difference from -10ms/+10ms.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_flasha:asLocalConnect = new asLocalConnect('Master');
addChild(lc_flasha);
lc_flasha.onSync(function () {
mc_demo.gotoAndPlay(1);
});
lc_master.sync('Master','Slave');
In this case the MovieClip mc_demo will be played synchronous when both flashfiles are available and synchronized.
This can take some seconds, depending on the amount of files to synchronous.
onHeartBeat(HeartBeat_function:Function) : void
This event will be called on every heartbeat, how often a heartbeat will send depends on the amount of the supervising asLocalConnect objects.
You can restart a movieclip to display the heartbeat graphically.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_flasha:asLocalConnect = new asLocalConnect('FlashA');
lc_flasha.onHeartBeat(function () {
bumper.gotoAndPlay(1);
});
lc_flasha.heartbeat('FlashB');
This example restart the movieclip Bumper on every heartbeat.
onStatus(onStatus_function:Function) : void
This Event will be trigger when asLocalConnection is sure if a send(...) was successful or if it was an failure
The StatusEvent will be passed to the function so that you can check the results with every detail.
/*
asLocalConnect Example from Markus Bordihn (http://markusbordihn.de)
*/
import de.markusbordihn.flash.as3.asLocalConnect;
var lc_flasha:asLocalConnect = new asLocalConnect('FlashA');
lc_flasha.onStatus(function (e:StatusEvent) {
trace('Result' + (e.level === 'status') ? 'succeeded' : 'failed');
});
lc_flasha.send('FlashB','test','123');
In this Example a message will be displayed deppending on the Event Level.
When the Event Level is "status" then the connection and the command was successful.
