Skip to main content
Participating Frequently
June 7, 2013
Question

AS3 Workers in Air HTML Environment?

  • June 7, 2013
  • 1 reply
  • 823 views

Hi there,

I've begun to look into the practicality of running AS3 workers in my JavaScript based desktop AIR project.  While I have not successfully nailed down a working implementation, it would seem that this is possible. 

However, am I right to assume that I would not be able to leverage the WebKit HTML environment from within a worker?

Lastly, is anyone actually using AS3 workers within a AIR HTML environment?  It seems to be an entirely unspoken feature within this context.  It it not documented because this is just not possible?

Thanks,

Mike

This topic has been closed for replies.

1 reply

Participating Frequently
June 8, 2013

Hi,

I've been doing some work trying to get an AS3 worker running from the webkit enviroment and am having a really strange problem.  As soon as I start the worker, the worker starts chewing through all available CPU.  No messages are being passed, it has just been started.

Here is an example of the worker:

package DDX
{
     import flash.events.Event;
     import flash.system.MessageChannel;
     import flash.system.Worker;
     
     public class Worker
     {
          private var mainToBack:MessageChannel;
          private var backToMain:MessageChannel;
          
          public function Worker()
          {
               var current:flash.system.Worker = flash.system.Worker.current;
               
               mainToBack = current.getSharedProperty("mainToBack");
               backToMain = current.getSharedProperty("backToMain");
               
               mainToBack.addEventListener(Event.CHANNEL_MESSAGE, onMainToBack);
          }
          
          private function onMainToBack(event:Event):void
          {
               if (mainToBack.messageAvailable) {
                    var msg:* = mainToBack.receive();
                    
                    if (msg == "SOLVE") {
                         backToMain.send("SOLVED");
                    }
               }
          }
     }
}

And here is how I set it up in JavaScript:

var workerLoader = new air.URLLoader(),
    mainToBack,
    backToMain,
    bgWorker,
    workerBytes;

workerLoader.dataFormat = air.URLLoaderDataFormat.BINARY;
workerLoader.addEventListener(air.Event.COMPLETE, function(event) {
    workerBytes = event.target.data;
    bgWorker = air.WorkerDomain.current.createWorker(workerBytes);

    mainToBack = air.Worker.current.createMessageChannel(bgWorker);
    backToMain  = bgWorker.createMessageChannel(air.Worker.current);

    backToMain.addEventListener(air.Event.CHANNEL_MESSAGE, function() {
        if (backToMain.messageAvailable) {
            var msg = backToMain.receive();
            console.log('RECEIVED ' + msg);
        }
    }, false, 0, true);

    bgWorker.setSharedProperty("backToMain", backToMain);
    bgWorker.setSharedProperty("mainToBack", mainToBack);

    // listen for worker state changes to know when the worker is running
    bgWorker.addEventListener(air.Event.WORKER_STATE, function(e) {console.trace(e)});
});
workerLoader.load(new air.URLRequest("assets/flash/DDXWorker2.swf"));

As soon as I start the worker, CPU starts being eaten up entierly:

bgWorker.start();

No messages being sent via the channel, just the worker started.

Any ideas?

Thanks,

Mike

Participating Frequently
June 9, 2013

To keep the conversation going with myself, I seem to have made a bit of progress.  A few comments for the sorry sucker working on a similar problem:

1) I was able to create a ActionScript project in Flash Builder that implemented the logic for creating and starting the worker.  Both the AS3 project SWF and the worker SWF were added into my HTML project.  From there, I was able to write some JS that interfaced with the AS project class to control the worker.

2) I was also able to take the worker generated from the AS project, and load it up in my JS code directly -- no run away CPU.  Which was fantastic!  One less level of abstraction to get at a worker form my JS code.

What I can't figure out is why the above AS worker that was compiled as a AS library project created problems for my CPU.  There must be something about how the worker gets compiled auto-magically by flash builder as part of the AS project that I am missing. 

Mike

Participating Frequently
June 13, 2013

Anyone have an idea why my WORKER_STATE event listener never fires when starting or terminating my worker?