January 28, 2006

Ask an FMS Guru #1: How do I use the File Object on the server side with FMS2?

Got a question here for the FMS Guru on "How do I use the file object on the server side with FMS2?"

So here's an answer :)

The file object on the server is actually quite powerful. Up until FMS2.0 there was no way to get a list of files, or write or read files directly from FCS. A lot of people wanted to be able to do this to get a list of FLV files, or MP3 files or something on the server that FCS was on. Most would use remoting and take advantage of PHP or Coldfusion to read a list and pass it back as an array perhaps. A pain.. that's for sure.

With the new file object API it's now possible to do quite a few things including getting lists of files, reading files and writing to them.

For an answer to the question, we are going to look at an example of writing a "login log" text file for every user that logs into a particular application.

First off we need to connect up to an application on FMS. So here is some basic client side code:


//create the netconnection object
var nc:NetConnection = new NetConnection();

//for the time being, stick in the onStatus
nc.onStatus = function(info){
      trace(info.code);
}

//connect up and pass in the username
nc.connect("rtmp:/file_object", "Graeme"); 
)

Now that is pretty simple, we're going to connect up to an application called "file_object" and pass in the username "Graeme".

On to the server side:


//setup the function that gets called when the client connects up
application.onConnect = function(clientObj, userName){
      //we're getting the client object and the userName that is passed in
      //make sure that the userName value exists, we're going to use it for the filename
      if(userName != undefined){
            //accept the connection for now
            application.acceptConnection(clientObj);
            //create the file object here
            var fileObj = new File(userName + ".txt");
            //check to see if the file actually exists already
            if(fileObj.exists){
                  //if so, then open the file with the mode "text" for text file, and "append" because we are going to add to it
                  fileObj.open("text", "append")
             }else{
                  //if not, then create it with the mode "create"
                  fileObj.open("text", "create");
             }
            //here is where we write in the text file
            fileObj.writeln(userName + " logged in on " + new Date());
            //and close up the file
            fileObj.close();
       }else{
            //no username, so reject them
            application.rejectConnection(clientObj);
       }
}

Now you save that code in your main.asc file, put it in the application folder and connect up. On the connect you should now see a brand new text file in the application directory with the user's username as the file name, and when they logged in. Log in more than once with the same user and it will add another line.

This method can be used for all kinds of things I think. Logging users in, logging views of a video etc etc.

I hope this helps understand one small part of the file object on the server side of FMS2.0.

Posted by Graeme at January 28, 2006 04:52 PM
 



Comments