I had quite a hard time figuring out how to call a function hosted on FMS (an .asc file) and how to call a function on the client from the server. I’m new to FMS (Flash Media Server) and there is not a ton of documentation, updated, for ActionScript 3.0.

So here is a simple example of interaction between server and client function. Obviously you first have to install FMS on your computer (I used the free developer version of FMS 3).

Installed? Let’s start: we will need two files, a document class for your .fla and an .asc (server side ActionScript) file.

.as file (document class): Interaction.as

package
{
import flash.display.MovieClip;
import flash.net.NetConnection;
import flash.net.Responder;
import flash.events.NetStatusEvent;

public class Interaction extends MovieClip
{
// Responder for call to server's
private var myResponder:Responder = new Responder(onReply);
private var nc:NetConnection;

public function Interaction():void
{
//Constructor
nc = new NetConnection();
nc.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );

// Connect to the server.
nc.connect("rtmp://localhost/Interaction");

//Allow method within th class to be called by the server side script
nc.client = this;

}

//Handle NetStatus
private function netStatusHandler( event:NetStatusEvent ):void
{
switch( event.info.code )
{
// Successfully connected to FMS, execute function
case "NetConnection.Connect.Success":
trace("connected");
callServerSideF();
break;
}
}

public function callServerSideF():void
{
//Call a server side function written on an .ASC file
nc.call("callFromClient", myResponder, "Server");
}

public function calledByServerSide(msg:String):void
{
//Function called by an .ASC file
trace("ASC have to say :"+msg);
}

// Responder function for nc.call()
private function onReply(result:Object):void
{
trace("Client have to say " + result);
}
}
}

Now the .asc file: main.asc

//Application is launched
application.onAppStart = function()
{
/* Allow debugging */
this.allowDebug = true;
}

//Client is connected
application.onConnect = function( client )
{

//Accept the connection
application.acceptConnection( client );

//Call the function calledByServerSide from AS3
application.clients[0].call( "calledByServerSide", null, "Hello Client :-)");

// Define new client function for a nc.call().
client.callFromClient = function( helloStr ) {
return "Hello Mr " + helloStr + ":-D~";
}
}

//Client disconnected
application.onDisconnect = function( client )
{
//Trace on the FMS Application console
trace( client+" is disconnected" );
}

Copy main.asc on your FMS application folder (I called it /Interaction/).

I was helped by a tutorial made by ‘Newtriks’ and published in WebDesigner magazine (files are available to download, issue 137).

Download the example.

Ahmet


  8 Responses to “Flash Media Server 3: calling a function from ASC to AS3
(Server – Client interaction)”

  1. Thanks for the reference Ahmet and glad the tutorial helped :)

  2. Hi Simon,
    that was really the minimum I could do, your tutorial was a great help!

  3. [...] days ago I started using FMS (Flash Media Server). Obviously I wanted to write my project in AS3 and realized that they [...]

  4. ahmet very thanks. have you video chat source code ? pls?

  5. Thanks so much for the missing link! Finally, my project has turned a corner.
    One thing I’m not clear on is the use of clients[0] in the server side code. If there are multiple clients, what happens?

  6. thx Ahmet!

    This thread helped a lot!
    Much better as the .call()-documentation from Adobe

  7. Thanks so much. it’s help me alot.

  8. Thanks 4 helping!

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>