Published in: ActionScript
These are the standard event handlers for a new Application using the FMS.
/* Between the time an instance is created and destroyed, a number of things can happen. To simplify how things work, they are divided into three sections: startup, midlife, and shutdown. Each application object is a singleton object. Each instance gets it's own application object which is an instance of the Application class. These are all the standard event handler method of the application object */ /*************************** Startup ***************************/ application.onAppStart = function ( ) { //A. onAppStart is where to Initialize counters, variables, id's, etc trace( "onAppStart> " + application.name + " is starting at " + new Date() ); //B. You can set up a Server-side shared object to synchronize clients this.so = SharedObject.get(application.name + ".com", true); //C. This is the proper way to set default variables. It allows for expansion if(this.so.getProperty("t" == undefined)){} if(this.so.getProperty("foo" == undefined)){} //Always assign unique ID's on the Server-Side because it's single threaded this.nextUserId = 0; }; application.onStatus = function (info) { trace("onStatus> info.level: " + info.level + ", info.code: " + info.code); trace("onStatus> info.description: " + info.description); trace("onStatus> info.details: " + info.details); }; application.onConnect = function (p_client, userName, password) { //A. Assign a uniqueID for any user who logs in //p_client.userId = this.nextUserId++; //B. Decide is a client needs a userName p_client.userName = userName; //C. Decide is you want to give a client user read/write access //p_client.writeAccess = "/public"; //p_client.readAccess = "/"; //D. Inform the user that they have made a success connection application.acceptConnection(p_client); trace("onConnect> client.ip: " + p_client.ip); trace("onConnect> client.agent: " + p_client.agent); trace("onConnect> client.referrer: " + p_client.referrer); trace("onConnect> client.protocol: " + p_client.protocol); }; application.onDisconnect = function (p_client) { //A. Clear any session variables that may pertain to a client user (SharedObject variables) trace("onDisconnect> client.userName: " + p_client.userName) trace("onDisconnect> disconnecting at: " + new Date( )); }; /*************************** Shutdown ***************************/ application.onAppStop = function (info) { //A. For when the app stops trace("onAppStop> application.name: " + application.name); trace("onAppStop> stopping at " + new Date( )); trace("onAppStop> info.level: " + info.level); trace("onAppStop> info.code: " + info.code); trace("onAppStop> info.description: " + info.description); }; /*************************** MidLife ***************************/ /* Below are methods that any client can call. You are also able to write such methods within onConnect like: /////////////////////////////////////////////////// application.onConnect = function(p_client){ p_client.changeText = function(p_client){ //Do Something } } /////////////////////////////////////////////////// The reason you may not want to do this is because every time a user connects, this function will be placed into memory. Therefore if many users connect, you could begin to fing issues with memory allocation so it's much more efficient to use the psuedo Javascript Class called prototype. */ Client.prototype.changeText = function(p_client){ } Client.prototype.getStreamLength = function(p_streamName) { trace("Stream.length: " + p_streamName + ", " + Stream.length(p_streamName)); return Stream.length(p_streamName); }
Comments
Subscribe to comments
You need to login to post a comment.

Very helpful. Thanks for sharing
One Question: How to implement a function in client side with AS3 that will be called by FMS2.0. I know in AS2, client side code is like:
}; Then, in Server side, I can invoke this client function by:application.onConnect = function(client) { // accept the new client's connection application.acceptConnection(client); client.call("someFunction", new someHandler()); }
someHandler = function(){ this.onResult = function(res){ trace("random num: " + res); } this.onStatus = function(info){ trace("failed and got:" + info.code); }
};
Do you know how to do that with AS3 in client? Thank you very much.
One Question: How to implement a function in client side with AS3 that will be called by FMS2.0. I know in AS2, client side code is like:
}; Then, in Server side, I can invoke this client function by:application.onConnect = function(client) { // accept the new client's connection application.acceptConnection(client); client.call("someFunction", new someHandler()); }
someHandler = function(){ this.onResult = function(res){ trace("random num: " + res); } this.onStatus = function(info){ trace("failed and got:" + info.code); }
};
Do you know how to do that with AS3 in client? Thank you very much.
One Question: How to implement a function in client side with AS3 that will be called by FMS2.0. I know in AS2, client side code is like:
}; Then, in Server side, I can invoke this client function by:application.onConnect = function(client) { // accept the new client's connection application.acceptConnection(client); client.call("someFunction", new someHandler()); }
someHandler = function(){ this.onResult = function(res){ trace("random num: " + res); } this.onStatus = function(info){ trace("failed and got:" + info.code); }
};
Do you know how to do that with AS3 in client? Thank you very much.
Sorry for the late reply.
One example where I needed to make a request to AS1 server-side code using AS3 was to retreive the length of an mp3. In my example, setNsDuration is actually a function which will receive a parameter from the FMS2.
//Retreive the Length of the Song from FMS and set it to a variable nc.call("getStreamLength", new Responder(setNsDuration), mp3:filename);
function setNsDuration(length:Number):void{ someVariable = length; }