Application Development Tips and Tricks > Application design and development > Designing for interdependence

 

Designing for interdependence

Because the client-side and server-side ActionScript code are part of the same application, they must work interdependently. One example of the interdependency between the client and server code is the server-side ActionScript call method, which acts differently according to which object it is associated with—a client-side NetConnection object or a server-side Client object.

For example, the code below shows how you could create a network connection on the client side, and then make a call to it from the server side:

// This is client-side ActionScript in the FLA
my_nc = new NetConnection();
my_nc.someClientMethod = function() 
{ 
	// code here
}
my_nc.connect("rtmp://hostname:port/appname");

// This is server-side ActionScript in the main.asc file
clientObj.call("someClientMethod");

The argument passed to clientObj.call must be a previously defined method of the client-side NetConnection object. This is because any method in the client code that may be called by the server must be a property of a client-side NetConnection object.

In contrast, suppose you use the call method from the client, to call a method on the server side:

// This is client-side ActionScript in the FLA
NetConnection.call("someServerMethod");

// This is server-side ActionScript in the main.asc file
client.prototype.someServerMethod = function()
{
	// code here
}

// The following code would also work
onConnect(newClient) 
{
	newClient.someServerMethod = function()
		{
			// code
		}
}

In this case, the argument passed to NetConnection.call must be a method of the server-side Client object that was previously defined in the main.asc file. This is because any method in the server code that may be called by the client must be a property of a server-side Client object.