View comments | RSS feed

Using the application.onDisconnect handler

The server calls the application.onDisconnect handler when a client disconnects from the application. You can add code to this handler that notifies all other clients about this event, as in the following example:

// On the server side you would have the following
application.onConnect = function(newClient, name)
{
    newClient.name = name;
    return true;
}

application.onDisconnect = function(client)
{
    for (var i = 0; i < application.clients.length; i++)
    {
        application.clients[i].call("userDisconnects", client,name);
    }
}

// On the client side you would have the following

nc = new NetConnection();
nc.userDisconnects= function (name) {
    trace(name + "quits");
}
nc.connect ("rtmp:/app_name", userName);

TIP

 

If you're using components, see Handling events in a component-based application.


Comments


wardy277 said on Nov 19, 2008 at 9:30 AM :
Hi,

I am working on a disconnection script for my game. I have managed to get a unique id to the server and the client fine, the problem is that when the application.onDisconnect = function is called(when client disconnects) i have added a call back to each of the clients using this code:

for (var i = 0; i < application.clients.length; i++){
application.clients[i].call("userDisconnects", clientObject.name);
}

on the client i have added a netConnection.userDisconnects as a function:

nc.userDisconnects = function(name) {
console.text += "\nPlayer "+name+" has disconnected";
}

This function does get called on disconnections but it doest seem to get the variable passed from the server, it is undefined. I have got the server tracing the clientObject.name correctly. It just doesnt pass the variable.

Any ideas? Could this be a security thing?


Chris
tech_writer_00 said on Nov 20, 2008 at 6:42 PM :
Try this, it works for me. In the client FLA file, I have an input text field called nameText, a dynamic text field called statusText, and a button called connectButton.

// Server-side script

application.onConnect = function(client, username){
client.username = username;
trace(client.username + ": onConnect");
return true;
}

application.onDisconnect = function(client){
for (var i = 0; i < application.clients.length; i++){
application.clients[i].call("userDisconnects", null, client.username);
}
trace(client.username + ": onDisconnect");
}

// Client-side script

nc = new NetConnection();

nc.userDisconnects = function(username) {
statusText.text = username + ": disconnected";
}

nc.onStatus = function(info:Object){
statusText.text = info.code;
}

connectButton.onPress = function() {
nc.connect("rtmp://localhost/testapp", nameText.text);
};

Hope that helps -- and I'll update the code in the Server-side ActionScript Language Reference, too!
Jody
tech_writer_00 said on Nov 20, 2008 at 6:51 PM :
PS: You can use "name" instead of "username" -- that wasn't the issue.

The issue was that you weren't passing null as the second parameter in your call to Client.call(). The syntax for Client.call() is

clientObject.call(methodName, [resultObj, [p1, ..., pN]])

If you want to pass optional parameters, you must pass in some value for the resultObj parameter, if you do not want a return value, pass null.

http://livedocs.adobe.com/flashmediaserver/3.0/hpdocs/00000260.html#72355
daffi said on Nov 25, 2008 at 2:47 AM :
It is not cliear to me if application.onDisconnect event is fired also when I do something like:

application.rejectConnection() inside the application.onConnect() function.

This is trivial for me as I have to increase/decrease users number.
tech_writer_00 said on Nov 25, 2008 at 10:47 AM :
Yes, when you call application.rejectConnection(client) inside an application.onConnect() handler, the application.onDisconnect() handler also fires.

Jody

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/fms/2/docs/00000052.html