Skip to content

Instantly share code, notes, and snippets.

@katopz
Created March 30, 2014 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katopz/9873681 to your computer and use it in GitHub Desktop.
Save katopz/9873681 to your computer and use it in GitHub Desktop.
Allocate ANE with Worker in constructor will throw error e.g. ReferenceError: Error #1065: Variable com.freshplanet.ane.AirAlert::AirAlert is not defined.
package
{
import com.freshplanet.ane.AirAlert.AirAlert;
import flash.display.Sprite;
import flash.events.Event;
import flash.system.MessageChannel;
import flash.system.Worker;
import flash.system.WorkerDomain;
import flash.utils.setInterval;
public class Main extends Sprite
{
protected var mainToWorker:MessageChannel;
protected var workerToMain:MessageChannel;
protected var worker:Worker;
public function Main()
{
// test alert-ane --------------------------------------------
// https://github.com/freshplanet/ANE-Alert/
// Defining your callbacks
var myCallback1:Function = function():void
{
trace("Callback 1");
};
// Display a one-button alert popup
AirAlert.getInstance().showAlert("My title", "My message", "OK", myCallback1);
// test hello world worker --------------------------------------------
// http://esdot.ca/site/2012/intro-to-as3-workers-hello-world
/**
* Start Main thread
**/
if (Worker.current.isPrimordial)
{
//Create worker from our own loaderInfo.bytes
worker = WorkerDomain.current.createWorker(this.loaderInfo.bytes);
//Create messaging channels for 2-way messaging
mainToWorker = Worker.current.createMessageChannel(worker);
workerToMain = worker.createMessageChannel(Worker.current);
//Inject messaging channels as a shared property
worker.setSharedProperty("mainToWorker", mainToWorker);
worker.setSharedProperty("workerToMain", workerToMain);
//Listen to the response from our worker
workerToMain.addEventListener(Event.CHANNEL_MESSAGE, onWorkerToMain);
//Start worker (re-run document class)
worker.start();
//Set an interval that will ask the worker thread to do some math
setInterval(function():void
{
mainToWorker.send("HELLO");
trace("[Main] HELLO");
}, 1000);
}
/**
* Start Worker thread
**/
else
{
//Inside of our worker, we can use static methods to
//access the shared messgaeChannel's
mainToWorker = Worker.current.getSharedProperty("mainToWorker");
workerToMain = Worker.current.getSharedProperty("workerToMain");
//Listen for messages from the server
mainToWorker.addEventListener(Event.CHANNEL_MESSAGE, onMainToWorker);
}
}
//Messages to the Main thread
protected function onMainToWorker(event:Event):void
{
var msg:* = mainToWorker.receive();
trace("onMainToWorker:"+msg);
//When the main thread sends us HELLO, we'll send it back WORLD
if (msg == "HELLO")
{
workerToMain.send("WORLD");
}
else if (msg == "ADD")
{
//Receive the 2 numbers and add them together
//var result:int = mainToWorker.receive() + mainToWorker.receive();
//Return the result to the main thread
workerToMain.send(mainToWorker.receive());
}
}
//Messages to the worker thread
protected function onWorkerToMain(event:Event):void
{
//Trace out whatever message the worker has sent us.
trace("[Worker] " + workerToMain.receive());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment