Welcome to the first in (hopefully) a long series of posts on the secrets of making games with Flash Media Server.  Flash Media Server is typically not mentioned when the Flash Gaming Community talks about Multiplayer Development.  I’m here to change that, and in order to understand what FMS is able to do, you MUST understand the Client Object.

I’ve noticed that most Flash Developers have gotten into the habit of doing something like this when they are setting up their NetConnection:

nc = new NetConnection( );
nc.client = this;
nc.connect (rtmpAddress);
nc.addEventListener (NetStatusEvent.NET_STATUS, fmsNCStatus);

Actually, a lot don’t even know about the nc.client = this; I think.

It’s time to take our ritalin and put the knives away, because school is in session!  First of all, what the hell is the client?

client property

client:Object [read-write]

Language Version : ActionScript 3.0
Runtime Versions : AIR 1.0, Flash Player 9

Indicates the object on which callback methods should be invoked. The default is this NetConnection instance. If you set the client property to another object, callback methods will be invoked on that object.

Um, yeah, that’s whatever, right?  Well, it’s actually awesome sauce, the keywords are Callback Methods Invoked, but wait, callbacks from where?  The server yo, the freakin’ server that’s where, and no, we are not using Shared Objects. I’ll explain why not later.

Since most flash devs have developed an allergy to AS1 I am going to provide a code template for you that you can use in most cases.  You might actually learn to kinda like AS1/JavaScript 1.5, because it’s way easy.

application.onAppStart = function()
	this.maxClients = 8;

application.onConnect = function(currentClient, username)
	 	var nClients = application.clients.length;

	 	if( nClients + 1 > application.maxClients )
		 	trace("Too many users, rejecting client.");
		 	this.rejectConnection( currentClient);
		 } else {
			 trace("Allow User");
			 currentClient.name = username;
			 application.acceptConnection( currentClient );
		currentClient.transmitChat = function(textChat)
			application.broadcastMsg("receiveTextChat", currentClient.name, textChat);

application.onDisconnect = function(currentClient)
	application.broadcastMsg("clientDisconnect", currentClient.name);

That is all the server side code you need for a basic chat application (I know, why is every example a chat?). On starting the app we limit the clients that can connect to 8, this will be very useful for gaming, because we’ll be looking at this later as a ChatRoom. And we’ll later setup another FMS based Application that will be our Authoritative Game Server which will keep track of the Games being played, and which ones are available for more players, or the ability to make your own Private Game. That will be gone over later in the series.

Back to the server code above, most of what we are looking at is inside the application.onConnect. That is the method that is called whenever anyone connects to the Server, and they pass in currentClient (The Current Client Object attached to the person connecting), and their username (A String that you get from your Client Application). Here’s how you would call that from your AS3:

public function initFMS(rtmpAddress:String, name:String):void{
        //Here we instantiate our ClientObject

         clientObject = new Object();

        //Here we setup our Callback Methods

	clientObject["receiveTextChat"] = remoteTextChat;
	clientObject["clientDisconnect"] = remoteClientDisconnect;

	//And finally our NetConnection

	nc = new NetConnection();
	nc.client = clientObject;
        nc.connect (rtmpAddress, name);
	nc.addEventListener (NetStatusEvent.NET_STATUS, fmsNCStatus);

public function transmitChat(textChat:String):void
	nc.call("transmitChat", null, textChat);

private function remoteTextChat(username:String, textChat:String):void
	currentTextChat = username+": "+textChat+"n";
	dispatchEvent(new Event(FMSConnectorExample.CHATRECEIVED));

Here’s what is going on, clientObject["receiveTextChat"] = remoteTextChat; sets up the Callback function that will be called from the server. The transmitChat method in the AS3 is doing a nc.call(“transmitChat”, null, String(textChat)); which calls what we setup on the server. The server code is:

currentClient.transmitChat = function(textChat)
application.broadcastMsg(“receiveTextChat”, currentClient.name, textChat);

This is where it gets cool, because application.broadcastMsg is calling receiveTextChat on all of the connected client’s clientObject. So what that does is allow for each client to call the server, pass a string, and then the server broadcasts the string back to all the clients with the sender’s name. Nice right?

Well, that’s cool and all, but how does this apply to games? With this Template you could send Player State Updates, Entire Objects that have Events/Positional Data/Names anything you want really. Just remember the more information you send the slower and more bandwidth intensive it gets. This is also how RTMFP/Flash P2P works as well. It’s all about the client object.

Next post will build off of this code, and will show you a basic game example via RTMP AND RTMFP.

Please leave comments on whether or not this was helpful, or if I need to take it even slower. I’ll be happy to answer as many questions as I possibly can.

Download the code for this tutorial:


7 Responses to “Game Development with FMS series: The Client Object.”

  1. Diego

    Hey!!! This was extremely helpfull !!! and a different approach!
    I really hope you continue with the Game Development with FMS series tutorials, looking forward to your next example!!
    Thanks a lot!

  2. PiratePete

    Thar be some fine code !

  3. Shirlene Alperin

    Hm ,.. Yes the best way is to just play like my friend Mason Anania he just play games and enjoy his life.

  4. videogame

    Lovely post dude, thanks. I love videogames.

  5. nemi

    Source code link is not working.

  6. Wes O'Haire

    The link to the source code now works. Thanks nemi for the heads up


Leave a Reply

Current day month ye@r *