00:00
00:00
Newgrounds Background Image Theme

TallBird just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: LocalConnection

3,899 Views | 22 Replies
New Topic Respond to this Topic

AS: LocalConnection 2006-07-18 15:46:15


AS:Main

Foreword
Ok, so I feeled like doing another tutorial, searched through the AS:Main and didn't find a tutorial about the LocalConnection class.
So, well, I made one:

What is the LocalConnection Class?
The LocalConnection class is used to send data from a .swf file to another .swf file without using any other complicated coding languages than ActionScript, no matter in what browsers or players the files are in.
All that is required is a .swf file that send information and one that listens to them.

Creating the Sending .swf
So, for the file that sends the information.
First you need to declare a variable for the class, like this:
var out:LocalConnection = new LocalConnection();
Ok, we got the class declared but at the moment this won't send any information at all.
To send information you use send(name:String, method:String, parameters);
Here is an example where the name of the connection is sName and the method is sMethod:
var out:LocalConnection = new LocalConnection();
out.send("sName", "sMethod");

To send parameters you will have to do like this:
var out:LocalConnection = new LocalConnection();
out.send("sName", "sMethod", 1, false, "three");

Creating the Receiving .swf
On with the creation of the file that will receive the parameters.
You have to declare a variable for the class in the receiving file aswell:
var inc:LocalConnection = new LocalConnection();
And now we have to create a function for the method that we sent the parameters with:
inc.methodName = function(params):Void {
trace("Method called");
};

Now to connect to the sender .swf we use connect(); with the connection name used in the sending swf:
inc.connect(connectionName);
So a example connecting from the send file example:
var inc:LocalConnection = new LocalConnection();
inc.sMethod = function(par1, par2, par3):Void {
trace(par1);
trace(par2);
trace(par3);
};
inc.connect(sName);

The receiving .swf continues to listen to connection when the connect(); method has been called, unless you close it, like this:
inc.close();

Sending and Receiving Across Other Websites
As default when you send and receive data through LocalConnection it is inside that domain.
Say you have the .swf on www.domain.com then it sends to the other movies that responds on that domain.
If you want to send across other domains you'll have to write the domain name before the connection name, separated by a semicolon:
out.send("domain.com:conName", methodName);
Never use the www. part here.

For the receiving part from other domains you'll have to set up which domains to allow.
This is done by allowDomain:
inc.allowDomain = function(domain:String){
return true;
}

That allows all domains.
Here's an example of a receiver that allows the domain www.domain.com and www.domain2.com:
inc.allowDomain = function(domain:String):Void {
return (domain == "domain.com" || domain == "domain2.com");
}

Example (.fla & .swf)

- GuyWithHisComp


BBS Signature

Response to AS: LocalConnection 2006-07-18 16:15:23


this sounds cool man!!
what can you use this for?

Response to AS: LocalConnection 2006-07-18 16:16:37


At 7/18/06 04:15 PM, phyconinja wrote: this sounds cool man!!
what can you use this for?

I don't know.
Maybe have so when the player mc walks out to the left he comes out to the right in the second .swf?


BBS Signature

Response to AS: LocalConnection 2006-07-19 03:54:49


At 7/18/06 04:16 PM, GuyWithHisComp wrote:
At 7/18/06 04:15 PM, phyconinja wrote: this sounds cool man!!
what can you use this for?
I don't know.
Maybe have so when the player mc walks out to the left he comes out to the right in the second .swf?

Probably very useful for security, since you could presumably place the second swf anywhere (ie, several folders deep) on a domain and put your main game code in it, then load it into the first swf, making it a lot harder for decompilers/hackers to find and analyse your code. I'll have to run some tests on this, it could be helpful in my current job


- - Flash - Music - Images - -

BBS Signature

Response to AS: LocalConnection 2006-07-19 04:05:05


This is usefull for chat rooms, right?

Response to AS: LocalConnection 2006-07-19 04:08:11


::bookmark::

i plan on using this when/if making a "music samples" page for my brother's website. somebody said to use it because i asked for a way to have a seperate preloader. then again it couldve been you too. im definitely gonna use it though regardless of what i use it to do.

Response to AS: LocalConnection 2006-07-19 05:01:22


i plan on using this when/if making a "music samples" page for my brother's website. somebody said to use it because i asked for a way to have a seperate preloader.

oh shit wait, let me get this outROFLROFLHAHAHHHAHOMGOOSEIMSUCHAFIDIOTLO
L

http://newgrounds.co../topic.php?id=394648
is what they were sayin.

me so stuupaaad

Response to AS: LocalConnection 2006-07-19 05:30:49


At 7/19/06 03:54 AM, Denvish wrote: Probably very useful for security, since you could presumably place the second swf anywhere (ie, several folders deep) on a domain and put your main game code in it, then load it into the first swf, making it a lot harder for decompilers/hackers to find and analyse your code. I'll have to run some tests on this, it could be helpful in my current job

Not really, it connects to .swfs that are actually running on the current PC at that exact time - so the user would have to be viewing the .swf with the game code and the main one at the same time. It could be used for user interaction on a website, like the user clicks on "Contact" in a flash menu on the left, the contact.html is loaded into an iframe and the swf on the right hand side changes to match a new colour scheme.. stuff like that.

You could also do this with SharedObjects, but that is more complex.


Sup, bitches :)

BBS Signature

Response to AS: LocalConnection 2006-07-19 08:30:54


At 7/19/06 05:30 AM, liaaaam wrote: Not really, it connects to .swfs that are actually running on the current PC at that exact time - so the user would have to be viewing the .swf with the game code and the main one at the same time.

Ah, OK, not particularly useful at all then really


- - Flash - Music - Images - -

BBS Signature

Response to AS: LocalConnection 2006-07-19 08:37:31


hey denvish can i do one on animated masks and a button that changes the dimensions of a graphic?


PM me for a Signature. | Add me on Steam: Sneakers25

Response to AS: LocalConnection 2006-07-19 08:56:55


At 7/19/06 08:30 AM, Denvish wrote: Ah, OK, not particularly useful at all then really

Yeah.. I guess you could make a debugging system with it, that would be pretty useful for a huge project. So an external swf outputs whatever is going on, if there are any errors, etc.


Sup, bitches :)

BBS Signature

Response to AS: LocalConnection 2006-08-09 10:58:04


if you can get it to connect to a variable number of other players, you could use it for a very good IM system, built to look exactly how you want...

I never did like MSN much any way...

Response to AS: LocalConnection 2008-02-18 10:44:07


Could be useful for high scores. Quite complex though.


OMG FISH

BBS Signature

Response to AS: LocalConnection 2008-02-18 11:13:21


What? Highscores? It's used to communicate between seperate SWF displayers, the browser and the desktop and AS2 and AS3.


BBS Signature

Response to AS: LocalConnection 2008-12-10 22:52:20


I think it would be mainly usefull for game to display stats, menu or map on a different "window" that the one where you play the game. You can also emulate the nintendoDS multiple screen feel. But still, it's usage seems kind of limited .

Response to AS: LocalConnection 2008-12-10 23:24:21


Wow great tut. but the only thing with really any use would be debugging.


Grah i feel so unknown, SK8MORE god damn :/ EvanHayes seems like a much more serious name than sk8more,so i changed it.

Response to AS: LocalConnection 2008-12-11 05:20:36


Bookmarking this topic. We just started doing some networking projects in our class using Java (local only), and I'm really enjoying it.

Also, am I right to assume I can't use IP Address/Socket, so that I can create a server SWF and client SWF? Like, I'll run the server SWF, then send the client SWF to a friend so we could communicate through SWFs? I hope we could, seeing as how you mentioned we can use URLs. :(

Or could someone tell me what I should be using instead of this that's AS only?

Response to AS: LocalConnection 2008-12-11 05:51:26


At 12/11/08 05:20 AM, WolfAkela wrote: Also, am I right to assume I can't use IP Address/Socket, so that I can create a server SWF and client SWF? Like, I'll run the server SWF, then send the client SWF to a friend so we could communicate through SWFs? I hope we could, seeing as how you mentioned we can use URLs. :(

That is correct. You can't use this as a socket server because this is only for talking between two local flash files. The domain part is because you can't communicate between two flash files running locally unless they are on the same server, or are allowed, for security reasons.

Or could someone tell me what I should be using instead of this that's AS only?

This is one of those times I will recommend haXe again. It has a very similar syntax to ActionScript so it's easy to pick up. You can compile to either flash player or Neko (among others). So you could write your flash game/app in haXe and compile to flash AND write your socket server in haXe and compile to Neko for server-side.

However, with Adobe releasing a new protocol using UDP sockets you will almost be able to do what you mentioned, although not server-side. Two flash files could directly communicate with each other, on different computers, without sending through a server, as long as they're both connected to the same server. See Stratus for more info.

Response to AS: LocalConnection 2008-12-11 05:53:39


At 12/11/08 05:51 AM, hesselbom wrote: The domain part is because you can't communicate between two flash files running locally unless they are on the same server, or are allowed, for security reasons.

That made no sense. It should be "You can't communicate between two flash files running on the same computer unless they're both running on the same domain or if they're allowed to communicate cross-domain as demonstrated above."

Response to AS: LocalConnection 2009-05-30 10:18:50


sry to bump up a old thread, but in trying to learn this, i made a little movment and chat thing. It works 100% on my computer, in my browsers ad runs problem free. However, as soon as i give one of the links to another computer on the same network as me (in my house) neither files receive anything! I tried this on multiple computers, but with no success.
heres the 2 links:
File A
File B

Also, it seems that you cant use the same method names, as only the first one opend will receive anything, am i doing something wrong?


AS2||AS3||Motox

Thanks to hdxmike for the sig :]

BBS Signature

Response to AS: LocalConnection 2009-05-30 10:30:34


No they work perfectly fine for me. It could just be your computer settings though.


BBS Signature

Response to AS: LocalConnection 2009-05-30 10:35:09


At 5/30/09 10:30 AM, atomoxic wrote: It could just be your computer settings though.

Like i said, it runs fine if i do it just on my computer, but as soon as i give one of the files to another computer it doesn't.

But what type of settings r u talking about? Maybe the problem is in fact there


AS2||AS3||Motox

Thanks to hdxmike for the sig :]

BBS Signature

Response to AS: LocalConnection 2009-05-30 10:56:23


Well it could be firewall settings or anything. Maybe check flashs global settings on those computers aswell.


BBS Signature