« Back To Articles List

How do I tell a user why their connection request was rejected?

Graeme Bull - Mar 25, 2007

Oh good, an easy one :). Got a question asker here asking how it would be to tell a user why they were rejected from connecting to the application.

The question: How do I tell a user why their connection request was rejected?

The answer:

So, the deal is that you have an application with a bit of security on it. Which is of course always a good idea because you never know when a malicious user is going to come along and create some huge FLV files or something on your server.

For example, say you stopped users if they didn't pass in a username when connecting to the server. On the server side you might write something like this:

application.onConnect = function(clientObj, userName){
//no userName value passed in? or an empty string?
if(userName == undefined || userName == ""){
application.rejectConnection(clientObj);
}else{
//looks good, they have passed something in
application.acceptConnection(clientObj);
}
}

Now, that isn't so robust and is relatively easy to get around.. but makes for a simple example on how to accept or reject a connection based on certain circumstances.

In the above case the user will get rejected and you can handle that event on the client side with the onReject event that gets passed back to the netConnection object, but you don't know why you were rejected. In our coding, we only have one reason why somebody would get rejected so it's easy to tell, but what if there were a bunch of options? Like maybe, the user is a banned user, or their password was wrong, or it's too early etc.

To pass back some information you would create an object that can hold the error message and pass that in the rejectConnection method. Something like this:

var errorObj = new Object();
//pass back a message of what is wrong
errorObj.msg = "Password is wrong";
//or even just pass back a predetermined code number that you think of
errorObj.code = 1;
application.rejectConnection(clientObj, errorObj);

Then, on the client side you could catch that like so:

nc.onStatus = function(info){
var code = info.code;
if(code == "NetConnection.Connect.Reject"){
//trace out the string message that was set
trace(info.application.msg);
//trace out the preset code that that was set
trace(info.application.code);

}
}

So, based on the information that you can get out of the information object in the onStatus event, you can now tell why you were rejected and incorporate that into the application by maybe retrying to a different server, or showing the password screen again or letting the user know they are banned, or whatever :)