THIS DEMO USE OUR FLASHCOMM SERVER. IT'S AVAILABLE ONLY A FEW HOURS EACH DAY.SORRY IF IT'S NOT AVAILABLE NOW.

TEXT CHAT

You can run this demo opening another copy of this page at your computer or at another machine and trying to chat, creating or not a group.

We have reserved an space at the Flash movie for an Atmosphere world. Atmosphere is a beta product from ADOBE and we have a course on this link about Flash+Atmosphere programming . At the ADOBE site you can find more informations.

But the Atmosphere world is not so important for our demo. We have created a text chat application. How did we do it?

We have seen in previous lesson that we can call server methods from the client and call client methods from the server. We use the instance of netConnection at the client side and the instance of Client at the server side.

But we can also, if we have a SharedObject instance (so), create a method at the client side using:

so.myMethod=function(msg){
//lines of code
}
and, at the server side to have:
application.so = SharedObject.get("so", false);
application.so.send("myMethod", msg);

Another important fact about applications having SharedObjects is: we can create different instances of the netConnection using a new string (inst2, by example) in the path:

client_nc = new NetConnection();
client_nc.connect("rtmp://domain-name/application/inst2", "userx");
and the SharedObject of this instance of the application will not be the SharedObject of another instance.

We will use this concepts now and, mixing all we have learned, we will create the application of text chat of the demo.

The program at the client is:

stop(); 
_root.buts.setEnabled(false); //buts is the SEND button
// Open connection to the server like TESTER and instance: public
client_nc = new NetConnection();
client_nc.connect("rtmp://domain-name/basichat/public", "TESTER");
_root.sysmsg.text = "Wait...Server sleeping?..."; 
_root.user.text=  "Hello,TESTER!";
_root.buts.setEnabled(true);
initialize(); //We will define this function now:
function initialize(){
 //Create a remote shared object 
  users_so = SharedObject.getRemote("users_so", client_nc.uri, false);
  users_so.connect(client_nc);
 //When we have a new userID, users_so is updated
 //and automaticaly we refresh the 'people' list box.
 users_so.onSync = function(list) {
  _root.people.removeAll();
  //Look to the server program ahead
  //Here we will not work using the "array of change" (list) but the "data array"
  for(var i in users_so.data){
   if(users_so.data[i] != null){
    _root.people.addItem(users_so.data[i]); //Adds the value  of each item not null
   }
  }
 }//end onSync
 //Function to be called by the server to populate the chat field
 users_so.msgFromSrvr = function(msg) {
  _root.history.text += msg; //history is the big chat field
 }
}//end initialize

// If connection is closed, clear history and the listbox
client_nc.onStatus = function(info) {
 if (info.description == "NetConnection.Connect.Closed" ) {
  history.text = "";
  _root.people.removeAll();
 }
}

function doConnect() { //Handler of the button
 if (butc.getLabel() == "NEW USER ID") {
  client_nc.close();	
  //Reconnect to the chat application like a new userID
  if(_root.gang.text=="")_root.gang.text="public"; //gang is the input field of group
  client_nc.connect("rtmp://domain-name/basichat/" + _root.gang.text, _root.user.text);
  _root.buts.setEnabled(true);
  butc.setLabel("DISCONNECT");
  initialize();
 } 
 else if(butc.getLabel() == "DISCONNECT"){
  // Close connection
  client_nc.close();
  _root.people.removeAll();
  _root.user.text="";  
  _root.buts.setEnabled(false);
  butc.setLabel("NEW USER ID");
 }
}

function doSend(){ //Handler of the button SEND
 if (length(_root.msg.text) > 0) {
 // Send the message text by calling the server function  
  client_nc.call("msgFromClient", null, _root.msg.text);
 }
 _root.msg.text = "";
}

//Function called by server  at the connection
client_nc.setHistory = function(msg) {
 _root.history.text = msg; //the messages in this shared object until now
}

client_nc.onStatus=function(info) {
 if(info.code!="NetConnection.Connect.Success"){
  _root.sysmsg.text = "Wait...Server sleeping?... ";
 }
 else{
  _root.sysmsg.text = "Success in the connection!";
 }
}

The program in the server is:

 
application.onAppStart=function(){
 // Get the server shared object 'users_so'
 application.users_so = SharedObject.get("users_so", false);
 // Initialize an history property to have the messages
 application.history = "";
}

application.onConnect=function(client, name){
 //Make this new client's name the user's name
 client.name = name;
 //Create a new atribute of "data" in shared object that is the 
 //property: name of the object: client
 application.users_so.setProperty(client.name, name);
 //Doing this we will have in the "data array" something like that:
 //             name      value     oldValue    code
 //           Mary.name   Mary      undefined   undefined
 //           Peter.name  null        Peter     delete
 //           Joe.name     Joe      undefined   undefined
 //
 //This array is not the array that is created in the changes but is its base. 

application.acceptConnection(client);
	
 //Call the client function 'setHistory,' and pass 
 //the initial history
 client.call("setHistory", null, application.history);

 //The client will call this function to get the server
 //to accept the message, add the user's name to it, and
 // send it back out to all connected clients.
 client.msgFromClient=function(msg) {	
  msg = this.name + ": " + msg + "\n";
  application.history += msg;
  application.users_so.send("msgFromSrvr", msg);
 }
}

application.onDisconnect=function(client){
 application.users_so.setProperty(client.name, null);
}

I know that is not easy to understand many lines of code like we have here, but it's very important that you try to do it.


PREVIOUS LESSON NEXT LESSON
T. CONTENTS HOMEPAGE