Occasionally I have the sense that I might be doing something as a workaround that is completely unnecessary, as though I'm jumping through hoops, listening for an exact sequence of one hundred events, and then squinting my eyes and tilting the monitor just right in order to assign text to a text field when I can achieve the same thing simply and directly with one line of code. All out of ignorance that there is a simpler way. This might be one of those occasions. If it is then it wouldn't be the first.
In this case I had a requirement to create a unique name to use when connecting a LocalConnection. The idea is that two .swf files need to communicate over this LocalConnection, and both of them need to know the name of the connection. Seems simple enough. I could just hard code the same value into both .swf files. However, the deal is that these two .swf files exist in the same HTML page, and I also would like to be able to open more than one instance of the HTML page in different browser windows. If the name of the connection is hard coded into the .swf files then it will work for one instance of the page, but subsequent instances will throw errors because the connection will already be in use. In such cases the error might be something like the following (which is particularly unhelpful in diagnosing the actual error since it doesn't really indicate what caused the error.)
Error #2044: Unhandled statusEvent:. level=error, code=
Note that you might see this error in other cases, and it doesn't necessarily indicate that the solution described in this post is appropriate. This error could be caused by lots of different scenarios. For example, you would see this error if you were trying to send requests across domains and the .swf files weren't properly configured (see allowDomain() in the LocalConnection documentation.) Probably the best way to determine the actual cause is to listen for status events on all LocalConnection object, and log the status codes. However, in this particular case the errors were because an .swf (in a new HTML window) was trying to establish a connection using the same name as a pre-existing connection.
One obvious solution to this would be to create a unique name using JavaScript, and then I can pass that in to each of the .swf files using FlashVars. That would allow each HTML page to create a unique name and pass in the same name to each of the .swf files, ensuring that those two .swf files can communicate over the same channel, but other .swf files in other pages wouldn't conflict. That would work except that in this particular case one of the .swf files is not embedded directly in the HTML page, and it does not have access to the JavaScript in the HTML page. This is because one of the two .swf files is embedded inside another plugin that is, itself embedded in the HTML, and that plugin doesn't have a way to pass a value from the HTML page to the Flash Player instance it embeds.
Therefore, the solution I'm using follows. Note that we'll refer to the .swf files as sending.swf and receiving.swf. In this case sending.swf is the instance embedded in the HTML page directly, and receiving.swf is embedded in the plugin.
- Initially receiving.swf calls connect() for a LocalConnection object using a hard coded value of _defaultConnection.
- Receiving.swf's local connection client defines a method called setNewConnectionName(), which accepts a string parameter. When this method is called it first calls close() for the LocalConnection object, and then it calls connect(), using the parameter value.
- Once both .swfs are loaded into their respective players sending.swf creates a new, unique connection name. You could use any algorithm to create a unique name. In this case I use the following: var connectionName:String = "_" + (new Date()).getTime();
- Then sending.swf calls send("_defaultConnection", "setNewConnectionName", connectionName); for the LocalConection object.
- After that point sending.swf always calls send() with the new, unique connection name.
Do you have a working example of the method you are describing?
[...] local connection name between your applications (swfs). Check out Joey’s blog on this task – unique local connection names. In this project summary, I will not reference a unique connection [...]