Introduction to server-side applications for Mesmotronic Multiuser Server

Mesmotronic Multiuser Server 2
Server-side applications can add vital functionality to multiuser applications and games created using Mesmotronic Multiuser Server 2, including the ability to load and save files, communicate between rooms and applications, and push data to clients.

A server-side application is a standard ActionScript 3 SWF like any other, except that it cannot display any graphics and extends the MultiuserServerApplication class, rather than Sprite or MovieClip. Applications can therefore be created using any ActionScript 3 IDE, including Flex/Flash Builder, Flash CS3/4 and FlashDevelop.

In this tutorial, we’re going to create a very simple server-side application which sends, on request, a random colour to the client which the client will then apply to the stage. As usual, we’re assuming you have a reasonable working knowledge of ActionScript 3 and have Mesmotronic Multiuser Server installed locally, with the API added to your library path.

The first step in creating any new server-side application is to create a new document class which extends MultiuserServerApplication. Your initial class structure would typically look something like this:

package
{
    import com.multiuserserver.server.MultiuserServerApplication;

    public class RandomColourApp extends MultiuserServerApplication
    {
        override public function main():void
        {
            // Entry point
        }
    }
}

The main entry point is called immediately after the application property of your class (inherited from MultiuserServerApplication) has been initialized, enabling you to communicate with the outside world, and should be used in preference to a class constructor. Although we won’t be doing anything here in this tutorial, this is where you would normally configure your application and add any event listeners you may require.

By default, all calls made to the server by clients, via the MultiuserServerClient call method, will invoke public functions of your application class. So, to allow clients to request a random colour, we need to add a function called getColour:

package
{
    import com.multiuserserver.server.MultiuserServerApplication;

    public class RandomColourApp extends MultiuserServerApplication
    {
        override public function main():void
        {
            // Entry point
        }

        public function getColour():uint
        {
            return Math.round(Math.random()*0xFFFFFF);
        }
    }
}

Now that the function is in place, you simply need to publish your class in the usual way and copy the resulting SWF, RandomColourApp.swf, to a folder of the same name within the applications directory of Mesmotronic Multiuser Server. Based on the default installation folder, this would be:

 C:\Program Files\Mesmotronic\Mesmotronic Multiuser Server\applications\RandomColourApp\

Once copied, your application is ready to go.

Now, all we need to do is create a simple client application to test that our server-side application works (the elements which relate to the server call are highlighted):

package
{
    import com.multiuserserver.events.MultiuserServerEvent;
    import flash.events.IOErrorEvent;
    import flash.events.MouseEvent;

    public class RandomColourClient extends Sprite
    {
        public var client:MultiuserServerClient;

        public function RandomColourClient()
        {
            client = new MultiuserServerClient();
            client.addEventListener(MultiuserServerEvent.CONNECT, onConnect);
            client.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
            client.delegate = this;
            client.connect("localhost", 2040, "RandomColourApp", 10);

            stage.addEventListener(MouseEvent.CLICK, onClick);
        }

        public function onConnect(event:MultiuserServerEvent):void
        {
            trace("Connected!");
        }

        public function onIOError(event:Event):void
        {
            trace("Connection Error.");
        }

        public function onClick(event:MouseEvent):void
        {
            client.call("getColour", null, "setColour");
        }

        public function setColour(colour:uint):void
        {
            graphics.clear();
            graphics.beginFill(colour);
            graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
        }
    }
}

Publish the class above and you will find that every time you click on the stage it will change to the colour received from the server!

Obviously, this is just scratching the surface of what’s possible with server-side applications, but it should give you a good idea of how basic client-server interaction can be implemented.

We’ll be taking a look at more advanced functionality at a later date, but if you’d like to find out more in the mean time, there are several examples included with your Mesmotronic Multiuser Server installation (follow the link on your start menu), which are also available as a separate download.