LcBroadcast - localConnection ActionScript 2 broadcasting class

Demo | Download | Documentation

LcBroadcast provides an easy mechanism to broadcast messages to different instances of the flash payer running on local machine. It addresses a limitation of the localConnection object, in that localConnection must know the connection name of each instance to communicate with. LcBroadcast is ideal in situations where you only want one instance of your movie preforming an action at any given time, for example an embedded video player, may tell all other instances to stop playback once its play button in pressed.

Usage

download and unpack the ZIP and place the com folder in your ActionScript class path. The following example shows a trivial setup example

import com.freesome.events.LcBroadcast;

	var my_lcb:LcBroadcast;
	
	//create an instance of LcBroadcast
        //the string passsed is any unique id for your application
        //this ensures that any other apps using LcBroadcast will not pickup your events

	my_lcb = new LcBroadcast("com.freesome.demo");		

	//create a new object to act as a listener.
	//lcBroadcast will broadcast two events 'onInit' and 'onBroadcast'	
	//in each case we just want to trace the events to the output window

	var listen = new Object();

	listen.onInit = function (ob:Object) { 
		for( var i in ob ) trace( "key: " + i + ", value: " + ob[ i ]) ;}			

	listen.onBroadcast = function (ob:Object) {
		for( trace) Main.dtrace( "key: " + i + ", value: " + ob[ i ]);}

	//add the listener
	my_lcb.addListener(listen);

	//broadcast the message “hello world”
	my_lcb.broadcast({msg:”hello world”});

The final line:

my_lcb.broadcast({msg:”hello world”});

- illustrates the use of an object for broadcast. In the example a single field is passed named msg, in this case containing the string “hello world”. However the fields created in this object are completely user defined. A common addition would be the internalID of the object, this would allow an object to filter out messages from itself. For example

my_lcb.broadcast({msg:”hello world”, id:my_lcb.internalID});

Then the onBroadcast handler would preform a check similar to

	if (ob.id == my_lcb.internalID) {
		//code for broadcast from self
	} else {
		//code for broadcast from others
	}

Compiling the example

Full source code for the demo.html and swf is provided. If you want to compile this example you will need the fantastic FlashDevelop open source flash environment. Alternately you should simply copy the actionscript from main.as into the timeline within the flash IDE.

awesome! Thanks for sharing

awesome! Thanks for sharing it!