Internal Event :: userLost

Availability:

SmartFoxServer PRO 1.2.1

Event name:

userLost

Description:

This event is fired each time a user is suddenly disconnected from the server.
The event is available for both Zone Level and Room Level extensions.

Zone Level extensions will receive the event when a user is leaving any room in that zone, while Room Level extensions will receive it only if the user is leaving that room.

NOTE: It is always reccomended to keep track of Users by using their unique id. For example you can keep a local list of users with their id as the key.
When a client leaves a room or it is disconnected you will always receive the userId of the User that was lost, so it will be very easy to handle the event this way.

Properties:

name   description type
roomIds   A list containing all the room ID(s) where the user was connected (Zone Level only) Java native array (*)
playerIndexes   A list containing the playerIndex for each room where the user was connected (Zone level only) Java native array (*)
uid   The id of the user that was lost java.lang.String(*)
oldPlayerIndex   The playerIndex assigned for the current room (Room Level only) java.lang.String(*)

(*) A note on data types: the parameters passed by the server events to the running extensions are all Java types.
You can use them transparently in Actionscript or cast them to AS native data types.
» javadoc for -> java.lang.String
» javadoc for -> Java array


» Zone Level Extension Notes:
A Zone Level Extension listens for all the events happening in the rooms within that Zone. When the "userLost" event is fired you will receive two extra parameters called roomIds and playerIndexes with the list of rooms the user was in and the its playerIndex for each room.

In the most simple scenario, where the user can only join one room at a time, both lists will only contain one element.
If your application allows clients to join more rooms simultaneously both lists will be contain data for each room.

The two lists are also synchronized so the playerIndexes[2] containd the playerIndex for the room in roomIds[2]

» Room Level Extension Notes:
A Room Level Extension listens for events happening in that room only, so handling a user disconnection is almost identical to handling the "userExit" event

Example:

// Handle server events
// We assume this is a Zone Level extension, and our application allows clients to join one room at a time.
//
// We keep a list of game rooms in an array called gameRooms, where each item is an object describing the status
// of the game. Also each gameRoom[] item has a userList array property.
//
// When a user disconnects we look for the gameRoom where the event was fired and remove the user from the local list.
// Also we should check if this affects the status of the game and send the proper notifications to the other users
// in the same room.
function handleInternalEvent(evtObj)
{
	if (evtObj.name == "userLost")
	{
		var uid = evtObj.userId						// id of the user that was lost
		var roomIds = evtObj.roomIds				// list of room id(s) where user was present
		var playerIndexes = evtObj.playerIndexs		// list of playerIndex(es) for each room
		
		// Get the roomId
		var rId = roomIds[0]						// Our app does not allow users to be present in more 
													// than one room at a time!
		// Get the playerIndex
		playerIndex = playerIndexes[0]
		
		// Get the game room
		var gameRoom = gameRooms[rId]
		
		// Get the user that was lost
		var lostUser = gameRoom.userList[uid]
		
		trace("User: " + lostUser.getName())
		
		// Remove user from list
		delete gameRoom.userList[uid]
		
		//
		// Here goes the code that handles the game logic...
		// ...
		//
		
		if (playerIndex == -1)
		{
			// User was a spectator ...
		}
		else if (playerIndex > 0)
		{
			// User was one of the players in this room ...
		}
	}
}

See also: