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

By | July 15, 2012

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.

[adsenseyu1]

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).

[adsenseyu1]

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/).

[adsenseyu1]

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

 
0 Kudos
Don't
move!

11 thoughts on “Flash Media Server 3: calling a function from ASC to AS3 (Server – Client interaction)

  1. Ahmet Post author

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

    Reply
  2. Pingback: A Web Technologist Adventure » Blog Archive » A Chat with FMS 3 coded in ActionScript 3.0

  3. melissa

    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?

    Reply
  4. Alex

    thx Ahmet!

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

    Reply
  5. Pingback: A Chat with FMS 3 coded in ActionScript 3.0 | Random.Thinking.ToString();

Thoughts?