2

I need to send a text from an embedded SWF (Web browser) to an AIR based desktop app. I did everything like explained in the documentation but I can't establish a connection.

Does anybody see what I did wrong or can point me to a working example?

From the SWF:

function startConnection(e:Event=null):void
{
var localConnection:LocalConnection 
localConnection = new LocalConnection(); 

localConnection.client = this; 
localConnection.allowDomain("app#com.example.desktop"); 

var textToSend = "Hello world! Source: http://www.foobar.com";
localConnection.send("app#com.example.desktop:connectionName", "methodName",textToSend); 
} 

From the AIR desktop app:

 function onBrowserInvoke (event:BrowserInvokeEvent):void{
    var localConnection:LocalConnection 
    localConnection = new LocalConnection(); 
    localConnection.client = this

    localConnection.allowDomain("example.com");
    localConnection.connect("connectionName");
    } 

Thank you. Uli

Uli
  • 2,625
  • 10
  • 46
  • 71
  • Are you sure that you really need specific domain? You can use "portable" local connection name beginning with "_", in your example it'll be "_connectionName" without domain specification before name. – fsbmain Jan 09 '13 at 14:14
  • Just tried it and it doesn't work either. What are other common pitfalls? – Uli Jan 09 '13 at 16:43
  • I've checked this code in Flash Builder and it works: Web: localConnection.send("_connectionName", "methodName",textToSend); AIR: localConnection.connect("_connectionName"); Don't forget to make public function methodName in the web swf. – fsbmain Jan 09 '13 at 18:54
  • You can try wild card in allow domain for testing as well: localConnection.allowDomain("*"); in both apps in case you testing not in debug mode. – fsbmain Jan 09 '13 at 19:05
  • It works! Thanks for the "*" wildcard suggestion! One thing I need to find out is how to send only to the Air app with the APP ID _com.example.desktop_ Is this part correct? `localConnection.allowDomain("app#com.example.desktop");` – Uli Jan 09 '13 at 20:30
  • 1
    Yes it's correct format app#appid:_myConnection for the given app id and it works for me: SWF: localConnection.allowDomain("app#airtest"); localConnection.connect("_myConnection"); AIR: localConnection.send("_myConnection", "methodName", textToSend); Note that connection name uses _ for unpredictable domain names. – fsbmain Jan 10 '13 at 07:14
  • Perfect, thanks! Everything is working now! – Uli Jan 10 '13 at 11:03
  • Ok, pleased to help you, I'll copy this to answers as well. – fsbmain Jan 10 '13 at 11:17

1 Answers1

2

The working code is:

AIR:
    var localConnection:LocalConnection = new LocalConnection();
    localConnection.send("_myConnection", "methodName", "Hello world! Source: http://www.foobar.com"); 
SWF:
    var localConnection:LocalConnection = new LocalConnection();
    localConnection.allowDomain("app#airtest"); //or use "*" wildcard to allow any domains and AIR applications
    localConnection.client = this;
    localConnection.connect("_myConne‌​ction");

Where airtest is the app id for AIR application. Use the _ symbol before local connection name for supporting unpredictable domain names (it'll work in debug mode and via http).

fsbmain
  • 5,267
  • 2
  • 16
  • 23
  • Can you elaborate on how the "_" underscore helps for unpredictable domain names? (Or point to a source that explains what it is used for) – chamberlainpi Aug 15 '14 at 14:28
  • 1
    yes, sure, take a look on live docs [Different domains with unpredictable domain names](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/LocalConnection.html) – fsbmain Aug 15 '14 at 14:39