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:
var nc:NetConnection = new NetConnection();
nc.onStatus = function(info){
trace(info.code);
}
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:
application.onConnect = function(clientObj, userName){
if(userName != undefined){
application.acceptConnection(clientObj);
var fileObj = new File(userName + ".txt");
if(fileObj.exists){
fileObj.open("text", "append")
}else{
fileObj.open("text", "create");
}
fileObj.writeln(userName + " logged in on " + new Date());
fileObj.close();
}else{
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