A lot of people are asking for a simple way to send private messages using flashcom. I first thought on doing it clientside but Peldi told me that doing it serverside is a much better possibility, he also gave me a great idea (you can read the post here).

I have found this way of doing it and is really simple, it also gives you some nice possibilities (checking if a username is taken) and now you can say goodbye to for loops with this example.

One thing to note is that this is a pretty advanced piece of code. You will need a good understanding of FlashCom Server and you cannot simply use this with pre-made components (we will try and cover that part in another tutorial).

You need an object that will include the usernames and that slots will have a reference to each client.

application.onAppStart = function(){
   // create the list object
   this.list = {};
}

application.onConnect = function(newClient, username){
   // include the username on the list
   this.list[username] = newClient;
   // this is just to keep a reference of the username
   newClient.username = username;
   this.acceptConnection(newClient);
}

application.onDisconnect = function(oldUser){
   // delete the username from the list
   this.list[oldUser.username] = null;
   delete this.list[oldUser.username];
}

Now, you can use this to build a method that will send a call to a specific client. For example:

newClient.privateMsg = function(user, msg){
   application.list[user].call("privateMsg", null, msg);
}


This will invoke a function 'privateMsg' on the specified client. I will leave that part up to you to finalise. Make sure you check the serverside and clientside actionscript dictionaries for details.

You can check if a username is already taken with something like this for example:

application.onConnect = function(newClient, username){
   if(this.list[username] == null){
      // the username isn't currently in use
      this.acceptConnection(newClient);
      // more code here
   }else{
      // username is already taken
      this.rejectConnection(newClient, {msg: "username is taken"});
   }
}


And thats all. Hope this helps someone!