0

I'm attempting to run a SWF in a browser and have it communicate to an AIR application that is bundled as an exe for windows.

In my AIR app (the receiver) I have the following

package
{
import flash.display.Sprite;
import flash.events.StatusEvent;
import flash.net.LocalConnection;
import flash.text.TextField;

/**
 * ...
 * @author
 */
public class Main extends Sprite
{
    private var _tf:TextField;

    public function Main()
    {
        var localConnection:LocalConnection = new LocalConnection();
        localConnection.addEventListener(StatusEvent.STATUS, handleConnectionStatus);
        localConnection.allowDomain("*");
        localConnection.client = this;
        localConnection.connect("_myConnection");

        _tf = new TextField();
        _tf.appendText("Creating local connection\n" + localConnection.domain + "\n");
        _tf.width = stage.stageWidth;
        _tf.height = stage.stageHeight;
        addChild(_tf);
    }

    private function handleConnectionStatus(e:StatusEvent):void
    {
        _tf.appendText("handleConnectionStatus\n");
        _tf.appendText(e.code + "\n");
        _tf.appendText(e.level + "\n");

    }

    public function testMethod():void
    {
        _tf.appendText("You wrote \n");
    }
}
}

And in the sender, the SWF on the web I have

package
{
import flash.display.Sprite;
import flash.events.AsyncErrorEvent;
import flash.events.Event;
import flash.events.SecurityErrorEvent;
import flash.events.StatusEvent;
import flash.net.LocalConnection;
import flash.text.TextField;

/**
 * ...
 * @author
 */
public class Main extends Sprite
{
    private var _tf:TextField;

    public function Main()
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // entry point

        _tf = new TextField();
        _tf.width = stage.stageWidth;
        _tf.height = stage.stageHeight;
        addChild(_tf);

        var localConnection:LocalConnection = new LocalConnection();
        localConnection.addEventListener(StatusEvent.STATUS, handleConnectionStatus);
        localConnection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, handleAsyncError);
        localConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleSecurityError);
        _tf.appendText("Creating local connection\n" + localConnection.domain + "\n");
        localConnection.send("_myConnection", "testMethod", "Hello world!");
    }

    private function handleSecurityError(e:SecurityErrorEvent):void
    {
        _tf.appendText("handleSecurtityError\n");
        _tf.appendText(e.text + "\n");
    }

    private function handleAsyncError(e:AsyncErrorEvent):void
    {
        _tf.appendText("handleAsyncError\n");
        _tf.appendText(e.error.message + "\n");
    }

    private function handleConnectionStatus(e:StatusEvent):void
    {
        _tf.appendText("handleConnectionStatus\n");
        _tf.appendText(e.code + "\n");
        _tf.appendText(e.level + "\n");
    }


}

}

I run the AIR app and then I run the SWF, but the SWF just prints out "error" for the event's level property. Everything I'm reading online says this should work. I try to run the SWF on localhost and from an actual website and it acts the same either way.

kamcknig
  • 883
  • 3
  • 9
  • 27
  • Dear, an AIR application borns to run as Desktop Application, when you create a project for a web, you must use Web application template. – Joe Taras Oct 13 '16 at 06:20
  • I have done that. As I said. if I run the web swf within the content debugger locally OR if I run it from a server, I get the same results. No connection. – kamcknig Oct 13 '16 at 12:50
  • I tried it as a normal AIR installer file and that also failed so it's not due to being bundled as an exe – kamcknig Oct 13 '16 at 14:53
  • @kamcknig I think you're being told that an SWF in browser cannot communicate with non-browser environment software (less possible via `LocalConnection` method). Likely a security issue where a browser app shouldn't control/affect outside software. What is your end goal, if this worked? Maybe alternative can be suggested. – VC.One Oct 13 '16 at 20:39
  • While that might be true, the documentation says if there is a security issue that it will through a SecurityError event which I am not receiving. Also, all research by googlling shows many posts where people say it's being done and it looks my code is doing what theirs does. Here is an example http://stackoverflow.com/questions/14234833/as3-localconnection-between-swf-and-air-desktop-app – kamcknig Oct 14 '16 at 17:48

0 Answers0