Influxis Flash Media Interactive Server 3.5 Hosting Specialists


Automatically Resolve a Client to a New Instance if the Room is at Maximum

PROBLEM: Too many users interacting all at once bogging down your application, and you do not want to reject the clients as the application fills up to your set limit.

GOAL: Start a fresh new version of the same application every time your application gets crowded.

SOLUTION: Using instances in FMS easily solves the problem. Keep one (1) copy of the application on the server and split it up into instances of the application.

Click Here to learn more about FMS instances >>

PROBLEM: Instances cannot be created manually, and you have no way of knowing how many instances the application will need to ultimately scale to in order to handle the crowds of people logging in.

GOAL: Create a system that can automatically create a new instance once a room fills up.

SOLUTION: Although there are many ways to achieve this, a simple and not so complex solution that works:

    1. Let the client connect to the first instance “room1″, and before allowing them to connect, check how many users there are currently in the room using the “application.clients.length” protocol on the server-side.

[main.asc]
application.onAppStart = function()
{
    /* this is an arbitrary number, change it to set
    your maximum as you desire. */
    this.maxClients = 2;
};                                        

application.onConnect = function( p_client )
{
    var nClients = this.clients.length;                                        

    trace( "client connecting: "+p_client );
    trace( "current client count: "+nClients );
    trace( "Max Clients: "+this.maxClients );                                        

    /* if adding this client to the amount of clients
    that are currently connected will surpass our limit,
    DENY else ALLOW */                                        

    if( nClients + 1 > this.maxClients ){
        trace("Too many users, rejecting client.");
        var e = new Object();
        e.code = "full";
        this.rejectConnection( p_client, e );
    } else {
        trace("Allow User");
        this.acceptConnection( p_client );
    }                                        

};

    2. If the length of the number clients plus the current client exceeds the limit you allowed per instance then reject the client with a code of “full”. Upon that rejection, the room number will increment and attempt to connect again to room2 and run the same process all over again.

[fla]
var nInstNum:Number = 1;
var sInst:String = "room" + nInstNum;
var bDoReconnect:Boolean = false;
var nWait:Number;                                        

var nc:NetConnection = new NetConnection();
nc.onStatus = function( p_o:Object ):Void
{
    var code:String = p_o.code;
    var app:Object = p_o.application;
    trace( code );                                       

    if( code == "NetConnection.Connect.Success" ) {
        trace( "You are connected to "+this.uri );
    }
     else if ( code == "NetConnection.Connect.Rejected" && app.code == "full" ) {
        //increment the room instance number.
        nInstNum++;
        trace( "Rejecting too many users, try room"+nInstNum );
        bDoReconnect = true;
    }
     else if( code == "NetConnection.Connect.Closed" && bDoReconnect ) {
        // the connection needs a few millisec's to clear out.
        nWait = setInterval( _root, "doCon", 700 );
        bDoReconnect = false;
    }
};
// connects to application.
function doCon():Void
{
    clearInterval( nWait );
    sInst = "room" + nInstNum;
    trace( "Connecting to "+sInst );                                       

    // here we use 1 application folder with multiple virtual instances.
    // the instances are incremented making multi/room1, multi/room2, etc..
    nc.connect( "rtmp://influxisaccount.rtmphost.com/multi/" + sInst );
};                                       

doCon();
    3. When the status handler receives a rejection and the code is verified as “full”, increment the instance number and set a variable to reconnect once the connection is fully closed. As soon as the onStatus fires the final “NetConnection.Connect.Closed” event, give the NetConnection a few milliseconds to clear and then call the connection code once again with the new instance “sInst”.
    4. Although the process sounds like it would take a long time, it is actually surprisingly fast considering that clients would not always stay, and letting new clients enter in-between instances as a room becomes available to them.

Source Files

Jerry Chabolla

One Response to “Automatically Resolve a Client to a New Instance if the Room is at Maximum”

  1. Not that I’m totally impressed, but this is more than I expected for when I found a link on SU telling that the info here is awesome. Thanks.

Leave a Reply