Accessibility

Flash Communication Server Article

 

Clustering Flash Communication Server for Live Webcasts


Table of Contents

Programming for Dual-Server Connections

So how can you do this? I'll lay it all out. The nice little MultiConnection.as file included in this tutorial is something I wrote to overcome this clustering problem. It is not all-inclusive, nor the end-all solution. It is a base to work from to eventually build a well-oiled program specific to your organization.

The MultiConnection.as file is a stripped-down version of the NetConnection object coupled with the NetStream object and the remote SharedObject object. So it attempts to combine and accomplish what those three objects do individually. There are a few drawbacks as far as status message interaction and the like that you can work around, but for the most part it works great. Now I'll dive into the actual code that makes it work.

Working with the New Class

Place the MultiConnection.as file in your class library that you have associated with your Flash authoring environment. If you haven't set one up, then simply copy the MultiConnection.as file to your computer and in the Flash authoring environment, click the Menu button in the Actions panel (it is located on the top right of the panel) and select Preferences. From there select ActionScript 2.0 Settings, and then create a new class path to the folder where you saved the MultiConnection.as file.

Once you have placed the MultiConnection.as file in your class library, open it and change the path declaration (com.novell.MultiConnection) in the class to match your path (your folder where you have the class saved). The default path will be com.novell and you should change it to whatever your path is. I'd recommend using the de facto declaration style com.domainname, but that's just me. If you use the editor in Flash MX 2004, then you can do a syntax check to make sure it validates. You should see no errors. Using this new class will resemble what you are used to with the NetConnection and NetStream objects. So let's start programming.

  1. Initially you need to either:

    • Put the main.asc file in an application directory on your servers, which will include your necessary server calls
    • Simply incorporate the server calls into the main.asc file you have for your specific application
  2. After uploading the new main.asc file incorporating the code, restart the application so that it uses this new main.asc file.
  3. Next, open a new Flash document and declare a new MultiConnection object as follows:
    import com.sitename.MultiConnection
    mc = new  MultiConnection();
    
  4. Then pass in an array of servers and applications you want to connect to.
    var server_array=new Array();
    server_array[0]="rtmp://67.50.48.147/test";
    server_array[1]="rtmp://server2.com/test";
    mc.connect(server_array);
    

    You can also put your array in short hand like so:
    mc.connect(['rtmp://server1.com/test','rtmp://server2.com/test']);
  5. At this point the script connects to the servers you specified and passes back status messages related to the connections. To see these status messages, you need to set up a status function to display the message:
    mc.onStatus = function(objIn){
    	trace(objIn.code);
    }

    If you set up a status function to trace out the output, you'll get the following message when you connect successfully:

    NetConnection.Setup.Success.

    Otherwise you'll see this message:

    NetConnection.Setup.Failed. 

    This means one of the servers hasn't responded in the specified 3-second timeout.

  6. After you have established and verified successful connections, you can set up NetConnections and shared objects.

    mc.onStatus = function(objIn){
    	if(objIn.code == "NetConnection.Setup.Success"){
    		trace(objIn.code);
    		mc.sharedObjectSetup ("sharedobject_so");
    		mc.setNetStreams();
    	}
    }
  7. Now set up a simple audio stream to go to the servers. Just add this code inside the success script.

    myMic = Microphone.get();
    mc.attachAudio(myMic);
    mc.nsStatus=function(objIn){
    	trace(objIn.code);
    }
    mc.publish ("webCastName","live");
  8. The following operators can also be used in conjunction with NetConnections—the function below is similar to the netConnection.call function:

    mc.callRemote("funcIn",callBackFunction,"serverToCall"[,arguments]);
    mc.ncClose();
  9. The following operators can also be used in conjunction with NetStreams:

    mc.attachVideo(myVid);
    mc.nsSend("servercall",[arguments]);
    mc.nsTime();
    mc.nsClose();
  10. To access shared object operators, simply call them by passing in the correct parameters and define sync functions. These include:

    mc.setSoValue ();
    mc.getSoValue ();
    mc.sharedobject_so_sync();
    

    An example might be:

    mc.setSoValue("sharedobject_so.data.userName”,”Foo”);

    And to retrieve the value:

    mc.getSoValue("sharedobject_so.data.userName”);

So the full client code you've written is:


import com.novell.MultiConnection
mc = new  MultiConnection();
mc.connect(['rtmp://server1.com/test','rtmp://server2.com/test']);
mc.onStatus = function(objIn){
	trace(objIn.code);
	if(objIn.code == "NetConnection.Setup.Success"){
		mc.sharedObjectSetup ("info_so",true);
		mc.setNetStreams();
		myMic = Microphone.get();
		mc.attachAudio(myMic);
		mc.nsStatus=function(objIn){
			trace(objIn.code);
		}
		mc.publish ("webCastName","live");
	}
}
mc.info_so_sync = function(){
	trace("so synced");
}

This code will connect to the servers and create a shared object named info_so on both servers, which will sync to the info_so_sync function. It also attaches a microphone to your NetStream object and starts streaming to your servers.

Where To Go from Here

So basically, you have taken the NetConnection, NetStream, and SharedObject objects and combined them to operate with multiple servers. Like I said earlier, this tutorial is not all-inclusive but merely a start on clustering multiple server applications. It should get you started, however, as you begin deploying large-scale, video-intensive applications.