Friday, April 11, 2008
Trying to mimic a part of Parleys.com / AIR integration
Friday, April 11, 2008
Update:I am an idiot. Rich Shupe pointed out to me on FlashCoders that I forgot the underscores in the connection name strings.
You see, that's important to supply when the SWFs are running in different domains, etc. Its a flag for the VM. Doh! And it's all specified there in the online documentation too. So please disregard the post from this point on...
After a bunch of playing around, I think I may be going about something the wrong way. Or else I'm simply missing something fairly simple.
This is what I am trying to do:
I would like a SWF in a web page to be able to detect and respond to a SWF running locally on a client's machine (standalone or an AIR application). Like what happens on the Parley's website with its AIR client (BENZ also known as Benjamin Dobler)... maybe I'll email you a link to this post.
The first thing I thought to try was localConnection and some polling (not efficient but its a proof of concept only for me).
When both SWFs are run locally or online, things work perfectly. When I try to do what I want, it fails. I am wondering if I'd need to use an online socket server to get this to work right (I hope not).
Anyway, here are my two classes, a Sender (web page) and receiver (localhost):
package net.ericd.utils
{
import flash.display.Sprite;
import flash.net.LocalConnection;
import flash.events.StatusEvent;
import flash.text.TextField;
import flash.utils.Timer;
import flash.events.TimerEvent;
/**
* This SWF will reside in a web page.
*/
public class LocalConnectionSender extends Sprite
{
private var conn:LocalConnection;
private var myTimer:Timer;
private var circle:Sprite;
public function LocalConnectionSender()
{
conn = new LocalConnection();
conn.allowDomain( "*", "localhost" );
conn.allowInsecureDomain( "*", "localhost" );
conn.addEventListener( StatusEvent.STATUS, onStatus );
myTimer = new Timer( 500, 0 );
myTimer.addEventListener( TimerEvent.TIMER, ping );
circle = new Sprite();
circle.graphics.beginFill( 0xFF6600, 1 );
circle.graphics.drawCircle( 50, 50, 10 );
circle.graphics.endFill();
circle.alpha = 0;
addChild( circle );
myTimer.start();
}
private function onStatus( event:StatusEvent ):void
{
switch( event.level )
{
case "status":
circle.alpha = 1;
break;
case "error":
circle.alpha = 0.3;
break;
}
}
private function ping( event:TimerEvent ):void
{
conn.send( "lifeConnection", "lcHandler", "hello" );
}
}
}
And now here is the receiver:
package net.ericd.utils
{
import flash.display.Sprite;
import flash.net.LocalConnection;
public class LocalConnectionReceiver extends Sprite
{
private var conn:LocalConnection;
/**
* This SWF will reside on the desktop, local.
*/
public function LocalConnectionReceiver()
{
conn = new LocalConnection();
conn.allowDomain( "*", "localhost" );
conn.client = this;
try
{
conn.connect( "lifeConnection" );
} catch ( error:ArgumentError )
{
trace( "Can't connect, name already in use." );
}
}
public function lcHandler( msg:String ):void
{
trace( "received data: " + msg );
}
}
}
So that's it really. Seems simple enough, but I'm not getting the love right now. I thought i had set this thing up correctly, and the whole "*" and "localhost" would get around the local or network only things in the Publish Settings. I played with those and didn't get different behavior.
I also mucked with "flash.events.SecurityErrorEvent" and didn't get any security setting errors reported back to me. Is this kind of thing simply impossible with the new security models?
Comments:
There are currently 1 Comments:
-
Benjamin said...“Hey Eric,
I saw that you solved it with the underscores but another way is to use
app#APPLICATION_ID:CONNECTION_NAME.PUBLISHER_ID
This is specific for all local connection running in the local AIR sandbox and the reason why it did not work with just the connection name...
Benz”


