What is NetConnection? Located in the flash.net package, NetConnection inherits from EventDispatcher and then Object. It is a simple way to open a connection to a remote server, call services from it and then implement the return values into your ActionScript 3.0 project. This works even in standalone Adobe Air applications. Although it is not the best option for larger projects, it is a simple way to call remote services from a server without having to set up Value Objects, let alone a complex system. Quoting directly from the Adobe site:
“The NetConnection class creates a bidirectional connection between a Flash Player or AIR application and a Flash Media Server application or between a Flash Player or AIR application and an application server running Flash Remoting. A NetConnection object is like a pipe between the client and the server. Use NetStream objects to send streams through the pipe.
Without Flash Media Server, you can use the NetConnection class to play video and MP3 files from a local file system or from a web server.”
As you can see, NetConnection already opens up an array of possibilities. In this specific case scenario, let me first start off by saying that I was implementing the MVC design pattern (Model View Controller). I was building a small Adobe Air Application using the Flex 4 SDK, and heavily relying on ActionScript 3.0 for most of the coding. My objective was to call remote services on three different remote servers, which would return values stating that “Yes, this server is online.” Then using the variables that it returned, set up a custom event that would fire off stating whether or not the specific server was online. My Controller would listen for the event, and upon receiving it call the respective function that would change my view to say “Let’s turn on the green button instead of the red button, because this server is online!” Here is the code to set up a connection for a specific server:
Step 1: Import related classes
You’re going to want to import flash.net packages; NetConnection, ObjectEncoding, and Responder.
Step 2: Implement variables and Constructor
Implement whatever public class variables you want, and then pass in an null IEventDispatcher object in the constructor function and call the super of that class. This is only to prevent any issues with dispatching events from this ConnectionController class.
Step 3: Create a function to check a server
Make sure the return type of this function is void. Inside this, create a private variable by declaring the var keyword, for an instance of NetConnection, and any other variables having to do with this one specific function. Declare the ObjectEncoding variable, and the connect variable in the new NetConnection variable you just declared.
Step 4: Call a function and set up dispatchers
Use _gateway.call(locationOfFunctionOnRemoteServer, new Responder(functionIfReturnValue, functionIfError), ….arguments); to make a call to the server, you can implement this line for however many functions you want to call in this one operation. After this, set up the nested functions that are called in the _gateway.call method, and fire off dispatchers with the related custom event based on a return from the server.
And there you have it, a simple way to call and get a return value from a remote server in AS3.