<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description></description><title>Owen@</title><generator>Tumblr (3.0; @owen-steamshift)</generator><link>http://owen.blogs.steamshift.com/</link><item><title>Multiple LocalConnections, or: a whole lot of handshakes</title><description>&lt;p&gt;&lt;a title="View 'Tennis_monsters' on Flickr.com" href="http://www.flickr.com/photos/70017615@N00/3384944014"&gt;&lt;img height="233" width="500" border="0" alt="Tennis_monsters" src="http://farm4.static.flickr.com/3612/3384944014_92d7822a67.jpg"/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I had a situation recently where I had an as2 swf being loaded into an as3 swf and needed to communicate back and forth between them. This was no problem, just using LocalConnection. What was more tricky was getting them to work, even if there were multiple instances of the same app running. Using a little ingenuity it is possible though :)&lt;/p&gt;
&lt;!--more--&gt;
&lt;p&gt;Let’s start with a normal local connection. That’s pretty easy, like this (You’d probably want to put in some error handlers, but I left those out for brevity):&lt;/p&gt;
&lt;p&gt;as3 swf code:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;
&lt;pre lang="actionscript" xml:lang="actionscript"&gt;_as3connection = new LocalConnection();
_as3connection.connect("as3connection");
_as3connection.client = this;

public function testConnection():void
{
	trace("yo! as3 ok!");
	_as3connection.send("as2connection", "testConnection");
}
&lt;/pre&gt;
&lt;/code&gt; as2 swf code: &lt;code&gt;
&lt;pre lang="actionscript" xml:lang="actionscript"&gt;_as2connection = new LocalConnection();
_as2connection.connect("as2connection");
_as2connection.testConnection = function () { trace("as2 test is aok"; }
_as2connection.send("as3connection", "testConnection");
&lt;/pre&gt;
&lt;/code&gt; What happens here is we set up the as3 local connection, then when the as2 swf is loaded in, it sets up it’s own local connection, then sends a message to the as3 connection, which then in turn calls back to the as2 connection, completing the circle. Got that?!&lt;/p&gt;
&lt;p&gt;However, because the swf’s were using named connections, the system would break if there were multiple instances of the application open, for example in separate browser windows. The second instance would try to open a connection which was already in use from the first instance and would fail. So, what we need is to get a unique connection for each pair of swfs.&lt;/p&gt;
&lt;p&gt;If we added an id number to the connection, that should do it, right?  Well, yes it sure would. The only slight snag is this: The as2 swf has no idea what the id the as3 swf is using, and can’t find out without using a LocalConnection to do so. There is a simple, yet cunning, solution to this, through using a handshake connection.&lt;/p&gt;
&lt;p&gt;Let’s look at the theory and then have a squizz at the code.  OK, so the as3 swf is going to search for an empty connection. We do it on the as3 swf, cause the error reporting in as2 LocalConnection is a bit shoddy and because the as3 swf is going to be the first one to load.&lt;/p&gt;
&lt;p&gt;First we setup our id var and then start trying connections. So we’d try appNameConn1, then appNameConn2 and so on, until we get one that doesn’t give an error. We now have an id number that we can use. Next, when the as2 swf has loaded, we open connections in both the as2 and as3 swfs called something like ‘handshake’. The as2 swf asks the as3 swf across this connection for the id number, which is passed back along the handshake connection. When the as2 swf receives it, it opens its own connection. If this is successful, we close the handshake connection on both sides. By doing this, it means that other instances of this same application can then use the handshake connection to establish their own unique appNameConn+id connections.  So here’s the code:  as3:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;
&lt;pre lang="actionscript" xml:lang="actionscript"&gt;// class vars
private var _connectionID:int;
private var _handshake:LocalConnection;
private var _connection:LocalConnection;

public static var CONNECTION_VALID:String = "connectionValid";

// in the setupConnection method
_handshake = new LocalConnection();
_handshake.connect("handshakeas3");
_handshake.client = this;

_connection = new LocalConnection();
_connection.client = this;
_connectionID = 0;
_tryConnect();

private function _tryConnect():void
{			
	try
	{
		//trace("trying connect", _connectionID);
		connect("as3connection"+_connectionID);
	}
	catch(error:Error)
	{
		//trace("error, connected already");
		_connectionID++;
		_tryConnect();
		return;
	}
			
	//trace("connected");
	dispatchEvent(new Event(CONNECTION_VALID));
}

public function startHandshake():void
{
	_handshake.send("handshakeas2", "completeHandshake", _connectionID);
	_handshake.close();	
}
&lt;/pre&gt;
&lt;/code&gt; When the connection valid event is fired, then we can load the as2 swf.  as2 swf code: &lt;code&gt;
&lt;pre lang="actionscript" xml:lang="actionscript"&gt;var id:Number;
var as2connection:LocalConnection;

var handshakeas2:LocalConnection = new LocalConnection();
handshakeas2.connect("handshakeas2");
handshakeas2.send("handshakeas3", "startHandshake");

handshakeas2.completeHandshake = function(passedId:Number):Void
{
	//trace("RECEIVED ID:"+passedId+" preloaded "+preloaded);
	handshakeas2.close();
	id = passedId;
	createConnection();
}

function createConnection():Void
{
	as2connection = new LocalConnection();
	as2connection.connect("as2connection"+id);
}
&lt;/pre&gt;
&lt;/code&gt; And that should do it. We now have 2 connections set up, as3connectionX and as2connectionX, where X is the same id number in both the swfs. Because we used a separate handshake connection, which is now closed, the next pair of swfs can use that connection to set up their own unique pairings.&lt;/p&gt;
&lt;p&gt;This all seems like a pretty long way round, but it does work. If you know of a better way (I’m so sure I’ve missed something really obvious!) then let me know!&lt;/p&gt;
&lt;p&gt;Owen&lt;/p&gt; 
&lt;p&gt;Addendum: While we’re on the subject, it’d be an idea to check out LocalConnectionService from Blitz Labs. I haven’t had cause to use it yet, but it looks like it is pretty cool: &lt;a href="http://labs.blitzagency.com/?p=650"&gt;http://labs.blitzagency.com/?p=650&lt;/a&gt;&lt;/p&gt;
&lt;!-- Technorati Tags Start --&gt;
&lt;p&gt;Technorati Tags:
&lt;a href="http://technorati.com/tag/actionscript" rel="tag"&gt;actionscript&lt;/a&gt;, &lt;a href="http://technorati.com/tag/localConnection" rel="tag"&gt;localConnection&lt;/a&gt;, &lt;a href="http://technorati.com/tag/multiple%20swfs" rel="tag"&gt;multiple swfs&lt;/a&gt;, &lt;a href="http://technorati.com/tag/flash" rel="tag"&gt;flash&lt;/a&gt;
&lt;/p&gt;
&lt;!-- Technorati Tags End --&gt;</description><link>http://owen.blogs.steamshift.com/post/89671800</link><guid>http://owen.blogs.steamshift.com/post/89671800</guid><pubDate>Wed, 25 Mar 2009 08:19:00 -0400</pubDate></item></channel></rss>
