Forum Topic: AS: LocalConnection

(1,551 views • 22 replies)

This topic is 1 page long.

<< < > >>
None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 7/18/06 03:46 PM

GuyWithHisComp LIGHT LEVEL 27

Sign-Up: 11/10/05

Posts: 4,008

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

None

phyconinja

Reply To Post Reply & Quote

Posted at: 7/18/06 04:15 PM

phyconinja EVIL LEVEL 25

Sign-Up: 09/18/04

Posts: 2,825

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


None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 7/18/06 04:16 PM

GuyWithHisComp LIGHT LEVEL 27

Sign-Up: 11/10/05

Posts: 4,008

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

None

Denvish

Reply To Post Reply & Quote

Posted at: 7/19/06 03:54 AM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,229

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

Questioning

Storm

Reply To Post Reply & Quote

Posted at: 7/19/06 04:05 AM

Storm EVIL LEVEL 05

Sign-Up: 05/19/06

Posts: 1,106

This is usefull for chat rooms, right?


None

flashmenow-kthx

Reply To Post Reply & Quote

Posted at: 7/19/06 04:08 AM

flashmenow-kthx NEUTRAL LEVEL 05

Sign-Up: 04/17/06

Posts: 491

::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.


None

flashmenow-kthx

Reply To Post Reply & Quote

Posted at: 7/19/06 05:01 AM

flashmenow-kthx NEUTRAL LEVEL 05

Sign-Up: 04/17/06

Posts: 491

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

None

liaaaam

Reply To Post Reply & Quote

Posted at: 7/19/06 05:30 AM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,533

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.


None

Denvish

Reply To Post Reply & Quote

Posted at: 7/19/06 08:30 AM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,229

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

None

Spiff

Reply To Post Reply & Quote

Posted at: 7/19/06 08:37 AM

Spiff LIGHT LEVEL 09

Sign-Up: 06/24/06

Posts: 1,095

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


None

liaaaam

Reply To Post Reply & Quote

Posted at: 7/19/06 08:56 AM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,533

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.


None

thecoshman

Reply To Post Reply & Quote

Posted at: 8/9/06 10:58 AM

thecoshman DARK LEVEL 11

Sign-Up: 06/11/06

Posts: 812

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...


None

Bobby444

Reply To Post Reply & Quote

Posted at: 2/18/08 10:44 AM

Bobby444 DARK LEVEL 05

Sign-Up: 12/03/07

Posts: 848

Could be useful for high scores. Quite complex though.

OMG FISH

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 2/18/08 11:13 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,383

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

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

grafik2d

Reply To Post Reply & Quote

Posted at: 12/10/08 10:52 PM

grafik2d NEUTRAL LEVEL 10

Sign-Up: 06/29/04

Posts: 165

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 .


None

EvanHayes

Reply To Post Reply & Quote

Posted at: 12/10/08 11:24 PM

EvanHayes DARK LEVEL 11

Sign-Up: 01/13/07

Posts: 1,833

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.


None

WolfAkela

Reply To Post Reply & Quote

Posted at: 12/11/08 05:20 AM

WolfAkela LIGHT LEVEL 08

Sign-Up: 12/19/05

Posts: 2,327

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?


None

hesselbom

Reply To Post Reply & Quote

Posted at: 12/11/08 05:51 AM

hesselbom NEUTRAL LEVEL 01

Sign-Up: 07/19/08

Posts: 307

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.


None

hesselbom

Reply To Post Reply & Quote

Posted at: 12/11/08 05:53 AM

hesselbom NEUTRAL LEVEL 01

Sign-Up: 07/19/08

Posts: 307

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."


None

Yambanshee

Reply To Post Reply & Quote

Posted at: 5/30/09 10:18 AM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,585

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

None

atomoxic

Reply To Post Reply & Quote

Posted at: 5/30/09 10:30 AM

atomoxic LIGHT LEVEL 10

Sign-Up: 02/16/09

Posts: 226

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

BBS Signature

None

Yambanshee

Reply To Post Reply & Quote

Posted at: 5/30/09 10:35 AM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,585

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

None

atomoxic

Reply To Post Reply & Quote

Posted at: 5/30/09 10:56 AM

atomoxic LIGHT LEVEL 10

Sign-Up: 02/16/09

Posts: 226

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

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 05:47 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!