500 users license
$700

Friday, October 16, 2009

UserNameExists

One reason for failure in a login attempt is UserNameExists, which happens if the ES4 thinks that somebody is already logged on using that username. How to prevent this from keeping your user from logging in depends on the application.

Random Usernames

If your client generates random usernames, you should be able to just generate a new one and try again. Some versions of ES4 require that you break the connection and create a new instance of Electroserver for a second login to work correctly.

If the random username is generated inside the LoginEventHandler, it's even easier. Use a loop to generate the usernames, and getApi().isUserLoggedIn(name) to check whether that one is in use. When you find one that is available, use that one. When you have tried some arbitrary number (say, 100) and haven't found a single good one, then fail the login; this prevents endless looping.

Password Validated Usernames

Often when there is a bug (or just slowness) in an application, or the Flash player for that matter, a user will simply refresh his browser to log in again, but the connection breaks in a dirty way causing the ES4 to think that the user is still logged in. Normally if you wait 5+ minutes the ES4 will disconnect the client as idle, however most users don't want to have to wait five minutes - they want back in the game now. If the user chooses his own username with no password, then it is possible that there already is a human being playing using that username. However, if login requires a valid password, we can assume that the duplicate username is a ghost.

One way to force a logout of the duplicate username so that the new user can login is this:
String username = context.getUserName();
if ( getApi().isUserLoggedIn( username ) ) {
// kick previous connection
EsObject obj = new EsObject();
obj.setString( "ReasonForKick", "Second login forces first one off" );
getApi().kickUserFromServer( username, obj );
}

This doesn't always work however, because the server waits to make sure that the user gets the message about being kicked off. If you are using the latest ES4 patch there is a new method available that sends the message about being kicked and then kicks immediately: evictUserFromServer.
String username = context.getUserName();
if ( getApi().isUserLoggedIn( username ) ) {
// kick previous connection
EsObject obj = new EsObject();
obj.setString( "ReasonForKick", "Second login forces first one off" );
getApi().evictUserFromServer( username, obj );
}

It's that easy.

Installing the Latest ES4 Patch

If you are already upgraded to ES4.0.6 and want to install the latest patch, download electroserver-4.0.7-a12.jar. Delete ElectroServer4.jar from your ES4 installation/server/lib, and place the new jar file in that folder. Restart ES4 and it's patched!



Labels: , , ,