Skip to main content
Inspiring
October 25, 2013
Question

AIR Worker?

  • October 25, 2013
  • 1 reply
  • 570 views

How does one create an AIR worker? I created a simple worker test and I find that the worker class doesn't seem to work unless I set the target to Flash Player. If I set the target to AIR then the resulting SWF gives no errors but simply doesn't work at all.

-Aaron

This topic has been closed for replies.

1 reply

Inspiring
October 28, 2013

Anyone? Let me explain what I'm doing in more detail and perhaps someone can tell me what I'm doing wrong.

First, I have the main app in Flash Pro as Main.fla, set to target AIR 3.7. I load and run the worker using the [Embed] tag and following the code example in the documentation.

Second, I have the worker app (for the moment a simple message channel echo test) as a FLA in Flash Pro. Now here's where it gets strange:

- If I set the worker FLA to target Flash Player 11.7, it all works as expected.

- If I set the worker FLA to target AIR 3.7, it does not work. There are no runtime errors in the main app, and it gets to the point in the Main app of the handling WorkerState.RUNNING, but I never get any messages back from the worker. I don't change any code. Here is my worker code:

package  {

          import flash.display.Sprite;

          import flash.system.MessageChannel;

          import flash.events.Event;

          import flash.system.Worker;

          import flash.system.WorkerDomain;

          import flash.utils.setInterval;

          public class FolderSizerWorker extends Sprite {

                    private var commandChannel:MessageChannel;

                    private var resultChannel:MessageChannel;

                    public function FolderSizerWorker() {

                              trace("isPrimordial:", Worker.current.isPrimordial)

                              //if(Worker.current.isPrimordial)

                                        //return;

                              commandChannel = Worker.current.getSharedProperty("commandChannel") as MessageChannel;

                              commandChannel.addEventListener(Event.CHANNEL_MESSAGE, commandMessageHandler);

                              resultChannel = Worker.current.getSharedProperty("resultChannel") as MessageChannel;

                    }

                    private function commandMessageHandler(e:Event):void {

                              trace("hello")

                              //var message:File = commandChannel.receive();

                              resultChannel.send("Whatever.");

                              setInterval(respond, 1000);

                    }

                    function respond(){

                              resultChannel.send("Whatever " + new Date().toString());

                    }

          }

}

And here is the relevent code on how I create the worker:

[Embed(source="Worker.swf", mimeType="application/octet-stream")]

private var WorkerSWF:Class;

worker = WorkerDomain.current.createWorker(new WorkerSWF(), true); // I also tried false for giveAppPrivileges (what is that for?)

workerCommandChannel = Worker.current.createMessageChannel(worker);

worker.setSharedProperty("commandChannel", workerCommandChannel);

workerResultChannel = worker.createMessageChannel(Worker.current);

workerResultChannel.addEventListener(Event.CHANNEL_MESSAGE, workerResultMessageHandler);

worker.setSharedProperty("resultChannel", workerResultChannel);

worker.addEventListener(Event.WORKER_STATE, workerStateHandler);

worker.start();

private function workerStateHandler(e:Event):void {

     if(worker.state == WorkerState.RUNNING)

          workerCommandChannel.send("START PLEASE");

}

Can anyone spot something wrong? Is it actually possible to create a worker using AIR, or does the worker itself have to be a plain Flash Player SWF?

-Aaron