As promised here is the second part of our tutorial. We will now take a closer look at how we can clear the chat history by clearing the server-side shared object that holds it and broadcasting this change to all connected users.

This tutorial presumes that you are using the pre-build communication components from Macromedia.

A fortunate fact is that all functionality to do what we are trying to achieve is already built into the components, all we need to take care of is how to call upon these functions.

Step one: Make sure clients are allowed to clear the history.
Take a look at the chat.asc file, it includes all the server-side code that works alongside your chat component (and after all that's where the chat history is displayed).

Line 35 of chat.asc should look like this
FCChat.prototype.allowClear = true; // Allow clients to clear history

If allowClear is set to false then make sure you change it to true.

Step two: Making sure you know what you are doing ;-)
The function that you will be calling in a minute is located from line 90 of chat.asc, it starts like this:
FCChat.prototype.clearHistory = function( client ) {
...
}

If you (somehow, anyhow) call this function it will first check if allowClear is set to true (which it is now), then it will clear the chat history by running these four lines of code
this.history_so.setProperty( "history", null );
this.history_so.flush();
delete this.history;
this.history = new Array;

and finally it will broadcast this change to all connected clients.
// Broadcast a clearHistory command to all clients
this.message_so.send( "clearHistory" );
return true;


So far that's very easy, you don't even have to write any code at all.

Step three: call the clearHistory() method
Easier said than done... but then again it's not that hard. I will show you one way of doing this very easily and it will allow you to clear the chat history by typing a 'secret' string into the input field of your application's chat window.

Line 73 of chat.asc contains the method sendMessage() and here it takes two parameters, one of which is mesg - the message string that has just been received and will now be sent to all clients. However before it gets sent we will intercept mesg, parse it and if the string matches our secret command then we will call the clearHistory() method.

This is easily done by simply adding the following piece of code on line 74 (and inside the sendMesage() method:
if (mesg.toLowerCase() == "secretstring"){
   this.clearHistory();
   }
else {
...
}


Basically all the original code will now be inside the else statement and will only be executed if the string does not match our secret command. However if you were to type "secretstring" (or whatever you have hard coded on line 74) then only this.clearHistory(); would run, resulting in the deletion of the chat history and a broadcast of this change to all connected users.

Moreover, none of the connected clients will ever see the secret string because it will not be broadcasted.

Remember that you must restart your application from the application inspector, this will reload the modified chat.asc file.

Hopefully this little tutorial will get you started. There are loads of other ways of calling clearHistory(), for example you could call it every time a user connects (but then again that's a bit silly) or every time the last user disconnects (makes more sense).

I leave that part up to you but the most obvious option is probably writing your own server-side function which you can call by pressing a pushbutton inside your client movie - for example you could have some kind of admin movie that is different to all the 'public' ones. For more information take a closer look at the call() method of the client-side communication actionscript dictionary.

Good luck and keep those suggestions coming in.

Check the first part of this tutorial
.