
Using FlashComm we can call server methods from the client and call client methods from the server. And transfer data.
To do this we need to create a file main.asc on the server, in the folder of the application.
At the client side the connection line of code can have many parameters who will be transfered to the server side:
client_nc.connect("rtmp://domain-name/app", par1, par2);
At the main.asc program we need to have the application.onConnection event-handler that will have parameters in the same order:
application.onConnect=function(newClient, rec1, rec2) {
//The newClient parameter is a name to be the
//reference-of-instance of an instance of the Client class.
//To give to this new client's property "name" the value of the first parameter:
newClient.name = rec1; //rec1 value is par1 value
//We need to accept the new client's connection
application.acceptConnection(newClient);
//Other lines of code...
}
From the server program we can call a method/event-handler in the client side. By example: we define in the client program:
client_nc.msgFromSrvr=function(msgr) {
var msgr;
_root.msg.text = msgr; //Put the string received in a field
}
And in the server program we have the lines:
var msg = "Hello from the server!";
newClient.call("msgFromSrvr",false, msg);
We can have many parameters - not only msg - that will be received in the same order.
Our exercice is simple: an echo plus a complementary message. Try the demo:
OUR FLASHCOMM SERVER IS AVAILABLE ONLY A FEW HOURS EACH DAY.TRY. AND SORRY IF IT'S NOT AVAILABLE NOW.
In the client movie we have a component Button (but) having the Click Handler: doConnect. Look to the complete code:
stop();
_root.msg.text ="Messages here!";
client_nc=new netConnection();
//Event handler for the button
function doConnect() {
if (but.getLabel() == "CONNECT") {
//The 2nd parameter - the input field value - is the FIRST STRING to be transfered to server
client_nc.connect("rtmp://domainname/app", user.text);
_root.msg.text = "Wait a little for a message. Trying to connect. ";
//Update button label
but.setLabel("DISCONNECT");
}
//If user wants to disconnect
else if (but.getLabel() == "DISCONNECT") {
//Close connection
client_nc.close();
but.setLabel("CONNECT");
_root.user.text = "";
_root.msg.text = "Disconnected";
}
}
//Function the server calls to send message back to this client.
client_nc.msgFromSrvr=function(msgr){
var msgr;
_root.msg.text = msgr;
}
// Handle status message if connection failed
client_nc.onStatus=function(info){
if(info.code!="NetConnection.Connect.Success"){
_root.msg.text = "Demo not available now! Try it later.";
}
}
In the file: main.asc we have:
application.onConnect=function(newClient, firstparameter) {
//Give this new client the same name as the user name from the input field
newClient.name=firstparameter;
//Accept the new client's connection
application.acceptConnection(newClient);
//Create a customized "Hello" message
//that the server will send to the client
var msg = "Hello! You are connected as: " + newClient.name;
//Call the client function and pass it the msg
newClient.call("msgFromSrvr", false, msg);
}
This client-server interation is very important. You need to understand it well.
PREVIOUS LESSON NEXT LESSON T. CONTENTS HOMEPAGE
