PDA

View Full Version : AIR (logger) needs to talk to AS2 SWF loaded from localhost


wvxvw
10-16-2008, 02:30 PM
Hi.
I have tried to write the simple AIR app that would log my AS2 SWF that I need to load from localhos. I'm guessing the problem is in security for LocalConnection, but don't know how to permit AS2 SWF to call LC in the AIR app.
Here's the code:
(AIR)
<?xml version="1.0"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
applicationComplete="handleAppComplete(event)">
<mx:Script>
<![CDATA[
import com.aditall.log.Logger;
import flash.events.Event;
import flash.events.StatusEvent;
import flash.net.LocalConnection;
import mx.core.WindowedApplication;

public var loggerLC:Logger;
//public var testLC:LocalConnection;

[Bindable]
public var loggerMessage:String;

public function handleAppComplete(evt:Event):void
{
logArea.width = (evt.target as WindowedApplication).width - 80;
logArea.height = (evt.target as WindowedApplication).height - 80;
//testLC = new LocalConnection();
//testLC.addEventListener(StatusEvent.STATUS, handleStatus);
loggerLC = new Logger();
loggerLC.addEventListener(Logger.LOG_RECEIVED, handleLogUpdate);
loggerLC.log(loggerLC.domain);
//testLC.send("aditallmixer", "log", "Hi!");
}

private function handleStatus(evt:StatusEvent):void
{
trace(evt.level);
}

private function handleLogUpdate(evt:Event):void
{
loggerMessage = loggerLC.logMessage;
}
]]>
</mx:Script>
<mx:TextArea text="{loggerMessage}" id="logArea" />
</mx:WindowedApplication>
If you uncomment the lines connected to testLC you will see that logger actually works, it just cannot communicate to the SWF on localhost
package com.aditall.log
{
import flash.events.Event;
import flash.events.StatusEvent;
import flash.net.LocalConnection;

[Event("logReceived")]

/**
* Logger class.
* @author wvxvw
*/
public class Logger extends LocalConnection
{
public static const LOG_RECEIVED:String = "logReceived";
private static const MIXER_LC_NAME:String = "aditallmixer";

private var _logMessage:String = "Connecting...";

public function Logger()
{
super();
client = this;
allowDomain("localhost", "http://localhost/local/MIXER/mixer_build_mtasc.swf");
allowInsecureDomain("localhost", "http://localhost/local/MIXER/mixer_build_mtasc.swf");
connect(MIXER_LC_NAME);
}

public function log(logStr:String):void { logMessage = logStr; }

[Bindable("logReceived")]
public function get logMessage():String { return _logMessage; }

public function set logMessage(s:String):void
{
trace("called "+s);
_logMessage = s;
dispatchEvent(new Event(LOG_RECEIVED));
}
}

}
(AS2)
....
if (!_lc)
{
_lc = new LocalConnection();
_lc.send(_lcName, LOG, _settings);
_lc.onStatus = Delegate.create(Tracer, onLCStatusDelegate);
_lc.allowDomain("app#com.aditall.log.logger");
}
....
_lc.send(_lcName, LOG, sender + JS_SEPARATOR + message);
....
static private function onLCStatusDelegate(infoObject:Object):Void
{
getURL(JS_TRACER + "[onStatus]" + JS_SEPARATOR + infoObject.level + " domain " + _lc.domain() + JS_CLOSE);
}
....
Here I'm trying to send a message to the LC in AIR logger, and if the attemt fails I print it into FireBug console... unfortunately it fails...

Any insights?

mattkenefick
10-16-2008, 02:39 PM
What Grant Skinner thinks:
http://www.gskinner.com/blog/archives/2007/07/swfbridge_easie.html

wvxvw
10-16-2008, 03:10 PM
I'm sorry, but this doesn't really answers my question... SWFBridge won't help, it does the same that my example does (i.e. fails to communicate). The problem is with the sendboxes, though I can't put my head around it... It seems to me that there should be a way to let desctop AIR application to receive calls from SWF loaded from some remote host (localhost in my case), but for whatever reason it doesn't...

You can specify the string "localhost" to allow calls to this SWF file from SWF files that are installed locally. Flash Player 8 introduced security restrictions on local SWF files. By default, a SWF file that is allowed to access the Internet cannot also have access to the local file system. If you specify "localhost", any local SWF file can access this SWF file.

Between content (SWF-based or HTML-based) in an AIR application and SWF content running in a browser
Which is exactly my case.

This is what I've found in manual, and this is what I'm actually trying to implement, however, either I'm doing something wrong, or there's mistake in the manual...

mattkenefick
10-16-2008, 03:36 PM
Check for a crossdomain policy file?

Perhaps it's looking and cannot find so it fails.
Here is a working sample.



<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" />
</cross-domain-policy>

wvxvw
10-16-2008, 03:47 PM
Thanks for tying to help, actually found it... for whatever reason it says nothing about that in the explanation on LocalConnection.send(), but comes out that if you're calling it from different domain, even though the domain allows you to access it, you need to put the domain name in front of the method name you call eg:
my AIR's application domain looks like this:
app#com.aditall.log.logger
so, I needed to call "log" method on AIR's LC object like this:
myLC.send("app#com.aditall.log.logger:log", "Hi!");

EDIT: Comes out that for some reason if you have LocalConnection name prefixed by __ (double underscore), than you don't need to put the full connection's domain name when calling send(). Many thanks to BlooDHounD (http://flasher.ru/forum/member.php?u=24652) for sharing this information.