new SFSEvent()

Developers never istantiate the SFSEvent class: only use its static properties.

The constants contained in this class are used to register the event listeners; when an event is dispatched, an object containing event-specific parameters is passed to the listener. See the documentation below for a description of the parameters available for each event.

Example

This example shows the generic approach to be implemented to listen to events; please refer to the specific event types for the parameters object content.

var sfs = null;

function init()
{
	// Create SmartFox client instance
	sfs = new SFS2X.SmartFox();

	// Add event listener for connection
	sfs.addEventListener(SFS2X.SFSEvent.CONNECTION, onConnection, this);

	// Connect to the server
	sfs.connect("127.0.0.1", 8080);
}

// Handle connection event
function onConnection(evtParams)
{
	if (evtParams.success)
		console.log("Connected to SmartFoxServer 2X!");
	else
		console.log("Connection failed. Is the server running at all?");
}

Properties

constant static

ADMIN_MESSAGE  string

The adminMessage event type, dispatched when the current user receives a message from an administrator user.

This event is caused by the AdminMessageRequest request sent by a user with administration privileges.

The object passed to the listener contains the following parameters:
Property Type Description
sender SFSUser An object representing the administrator user who sent the message.
message string The message sent by the administrator.
data SFSObject A SFSObject containing custom parameters which might accompany the message.

Example

This example sends an administration message to all the users in the Zone; it also shows how to handle the related event.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.ADMIN_MESSAGE, onAdminMessage, this);

	// Set the message recipients: all users in the Zone
	var recipMode = new SFS2X.MessageRecipientMode(SFS2X.MessageRecipientMode.TO_ZONE, null);

	// Send the administrator message
	sfs.send(new SFS2X.AdminMessageRequest("Hello to everybody from the Administrator!", recipMode));
}

function onAdminMessage(evtParams)
{
	console.log("The administrator sent the following message: " + evtParams.message);
}
See also
AdminMessageRequest
SFSEvent.MODERATOR_MESSAGE
constant static

CONNECTION  string

The connection event type, dispatched when a connection between the client and a SmartFoxServer 2X instance is attempted.

This event is fired in response to a call to the SmartFox.connect() method.

The object passed to the listener contains the following parameters:
Property Type Description
success boolean The connection result: true if a connection was established, false otherwise.

Example

This example starts a connection.

function someMethod()
{
	sfs = new SFS2X.SmartFox();
	sfs.addEventListener(SFS2X.SFSEvent.CONNECTION, onConnection, this);

	sfs.connect(127.0.0.1, 8080);
}

function onConnection(evtParams)
{
	if (evtParams.success)
		console.log("Connection established");
	else
		console.log("Connection failed");
}
See also
SmartFox#connect
SFSEvent.CONNECTION_LOST
constant static

CONNECTION_LOST  string

The connectionLost event type, dispatched when the connection between the client and the SmartFoxServer 2X instance is interrupted.

This event is fired in response to a call to the SmartFox.disconnect() method.

The object passed to the listener contains the following parameters:
Property Type Description
reason string The reason of the disconnection, among those available in the ClientDisconnectionReason class.

Example

This example handles a disconnection event:

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.CONNECTION_LOST, onConnectionLost, this);
}

function onConnectionLost(evtParams)
{
	var reason = evtParams.reason;

	if (reason != SFS2X.ClientDisconnectionReason.MANUAL)
	{
		if (reason == SFS2X.ClientDisconnectionReason.IDLE)
			console.log("A disconnection occurred due to inactivity");
		else if (reason == SFS2X.ClientDisconnectionReason.KICK)
			console.log("You have been kicked by the moderator");
		else if (reason == SFS2X.ClientDisconnectionReason.BAN)
			console.log("You have been banned by the moderator");
		else
			console.log("A disconnection occurred due to unknown reason; please check the server log");
	}
	else
	{
		// Manual disconnection is usually ignored
	}
}
See also
SmartFox#disconnect
ClientDisconnectionReason
SFSEvent.CONNECTION
constant static

EXTENSION_RESPONSE  string

The extensionResponse event type, dispatched when data coming from a server-side Extension is received by the current user.

Data is usually sent by the server to one or more clients in response to an ExtensionRequest request, but not necessarily.

The object passed to the listener contains the following parameters:
Property Type Description
cmd string The name of the command which identifies an action that should be executed by the client. If this event is fired in response to a request sent by the client, it is a common practice to use the same command name passed to the request also in the response.
params SFSObject A SFSObject containing custom data sent by the Extension.
room SFSRoom An object representing the Room which the Extension is attached to (for Room Extensions only).

Example

This example sends a command to the Zone Extension; it also handles responses coming from the Extension by implementing the EXTENSION_RESPONSE listener (the same command name is used in both the request and the response).

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.EXTENSION_RESPONSE, onExtensionResponse, this);

	// Send two integers to the Zone extension and get their sum in return
	var params = new SFSObject();
	params.putInt("n1", 26);
	params.putInt("n2", 16);

	sfs.send(new SFS2X.ExtensionRequest("add", params));
}

function onExtensionResponse(evtParams)
{
	if (evtParams.cmd == "add")
	{
		var responseParams = evtParams.params;

		// We expect a number called "sum"
		console.log("The sum is: " + responseParams.get("sum"));
	}
}
See also
ExtensionRequest
constant static

INVITATION  string

The invitation event type, dispatched when the current user receives an invitation from another user.

This event is caused by the InviteUsersRequest and CreateSFSGameRequest requests; the user is supposed to reply using the InvitationReplyRequest request.

The object passed to the listener contains the following parameters:
Property Type Description
invitation SFSInvitation An object representing the invitation received by the current user.

Example

This example receives an invitation and accepts it automatically; in a real case scenario, the application interface usually allows the user choosing to accept or refuse the invitation, or even ignore it.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.INVITATION, onInvitationReceived, this);
	sfs.addEventListener(SFS2X.SFSEvent.INVITATION_REPLY_ERROR, onInvitationReplyError, this);
}

function onInvitationReceived(evtParams)
{
	// Let's accept this invitation
	sfs.send(new SFS2X.InvitationReplyRequest(evtParams.invitation, SFS2X.InvitationReply.ACCEPT));
}

function onInvitationReplyError(evtParams)
{
	console.log("Failed to reply to invitation due to the following problem: " + evtParams.errorMessage);
}
See also
InviteUsersRequest
InvitationReplyRequest
SFSEvent.INVITATION_REPLY
constant static

INVITATION_REPLY  string

The invitationReply event type, dispatched when the current user receives a reply to an invitation he sent previously.

This event is caused by the InvitationReplyRequest request sent by the invitee.

The object passed to the listener contains the following parameters:
Property Type Description
invitee SFSUser An object representing the user who replied to the invitation.
reply number The answer to the invitation among those available as constants in the InvitationReply class.
data SFSObject A SFSObject containing custom parameters, for example a message describing the reason of refusal.

See the example provided in the INVITATION constant description.

See also
InvitationReplyRequest
InvitationReply
SFSEvent.INVITATION
SFSEvent.INVITATION_REPLY_ERROR
constant static

INVITATION_REPLY_ERROR  string

The invitationReplyError event type, dispatched when an error occurs while the current user is sending a reply to an invitation he received.

This event is fired in response to the InvitationReplyRequest request in case the operation failed.

The object passed to the listener contains the following parameters:
Property Type Description
errorMessage string A message containing the description of the error.
errorCode number The error code.

See the example provided in the INVITATION constant description.

See also
InvitationReplyRequest
SFSEvent.INVITATION_REPLY
SFSEvent.INVITATION
constant static

LOGIN  string

The login event type, dispatched when the current user performs a successful login in a server Zone.

This event is fired in response to the LoginRequest request.

The object passed to the listener contains the following parameters:
Property Type Description
user SFSUser An object representing the user who performed the login.
data SFSObject A SFSObject containing custom parameters returned by a custom login system, if any.

Example

This example performs a login in the "BasicExamples" Zone:

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.LOGIN, onLogin, this);
	sfs.addEventListener(SFS2X.SFSEvent.LOGIN_ERROR, onLoginError, this);

	// Login
	sfs.send(new SFS2X.LoginRequest("FozzieTheBear", "", "BasicExamples"));
}

function onLogin(evtParams)
{
	console.log("Login successful!");
}

function onLoginError(evtParams
{
	console.log("Login failure: " + evtParams.errorMessage);
}
See also
LoginRequest
constant static

LOGIN_ERROR  string

The loginError event type, dispatched if an error occurs while the user login is being performed.

This event is fired in response to the LoginRequest request in case the operation failed.

The object passed to the listener contains the following parameters:
Property Type Description
errorMessage string A message containing the description of the error.
errorCode number The error code.

See the example provided in the LOGIN constant description.

See also
LoginRequest
SFSEvent.LOGIN
constant static

LOGOUT  string

The logout event type, dispatched when the current user performs logs out of the server Zone.

This event is fired in response to the LogoutRequest request.

No parameters are available for this event object.

Example

This example performs a logout from the current Zone.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.LOGOUT, onLogout, this);

	// Logout
	sfs.send(new SFS2X.Requests.System.LogoutRequest());
}

function onLogout(evtParams)
{
	console.log("Logout executed!");
}
See also
LogoutRequest
SFSEvent.LOGIN
constant static

MMOITEM_VARIABLES_UPDATE  string

The mmoItemVariablesUpdate event type, dispatched when an MMOItem Variable is updated in an MMORoom.

This event is caused by an MMOItem Variable being set, updated or deleted in a server side Extension, and it is received only if the current user has the related MMOItem in his Area of Interest.

The object passed to the listener contains the following parameters:
Property Type Description
room MMORoom The MMORoom where the MMOItem whose Variables have been updated is located.
mmoItem MMOItem The MMOItem whose variables have been updated.
changedVars Array of string The list of names of the MMOItem Variables that were changed (or created for the first time).

Example

This example shows how to handle the MMOItem Variable update.

function onMMOItemVarsUpdate(evtParams)
{
	var changedVars = evtParams.changedVars;
	var item = evtParams.mmoItem;

	// Check if the MMOItem was moved
	if (changedVars.indexOf("x") != -1 || changedVars.indexOf("y") != -1)
	{
		// Move the sprite representing the MMOItem on screen
		...
	}
}
See also
MMOItemVariable
constant static

MODERATOR_MESSAGE  string

The moderatorMessage event type, dispatched when the current user receives a message from a moderator user.

This event can be caused by either the ModeratorMessageRequest, KickUserRequest or BanUserRequest requests sent by a user with at least moderation privileges. Also, this event can be caused by a kick/ban action executed through the SmartFoxServer 2X Administration Tool.

The object passed to the listener contains the following parameters:
Property Type Description
sender SFSUser An object representing the moderator user who sent the message.
message string The message sent by the moderator.
data SFSObject A SFSObject containing custom parameters which might accompany the message.

Example

This example sends a moderator message to all the users in the last joned Room; it also shows how to handle the related event.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.MODERATOR_MESSAGE, onModeratorMessage, this);

	// Set the message recipients: all users in the current Room
	var recipMode = new SFS2X.MessageRecipientMode(SFS2X.MessageRecipientMode.TO_ROOM, sfs.lastJoinedRoom);

	// Send the moderator message
	sfs.send(new SFS2X.ModeratorMessageRequest("Hello everybody, I'm the Moderator!", recipMode));
}

function onModeratorMessage(evtParams)
{
	console.log("The moderator sent the following message: " + evtParams.message);
}
See also
ModeratorMessageRequest
KickUserRequest
BanUserRequest
SFSEvent.ADMIN_MESSAGE
constant static

OBJECT_MESSAGE  string

The objectMessage event type, dispatched when an object containing custom data is received by the current user.

This event is caused by an ObjectMessageRequest request sent by any user in the target Room.

The object passed to the listener contains the following parameters:
Property Type Description
sender SFSUser An object representing the user who sent the message.
message SFSObject The content of the message: a SFSObject containing the custom parameters sent by the sender.

Example

This example sends the player's avatar movement coordinates and handles the respective event (note: the myAvatar instance is supposed to be the user sprite on the stage, while the getUserAvatar method retrieves the sprite of other users' characters).

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.OBJECT_MESSAGE, onObjectMessage, this);

	// Send my movement to all players
	var dataObj = new SFSObject();
	dataObj.putInt("x", myAvatar.x);
	dataObj.putInt("y", myAvatar.y);

	sfs.send(new SFS2X.ObjectMessageRequest(dataObj));
}

private function onObjectMessage(evtParams)
{
	var dataObj = evtParams.message; // This is a SFSObject!

	var sender = evtParams.sender;
	var avatar = getUserAvatar(sender.id);

	avatar.x = dataObj.get("x");
	avatar.y = dataObj.get.("y");
}
See also
ObjectMessageRequest
constant static

PING_PONG  string

The pingPong event type, dispatched when a new lag value measurement is available.

This event is fired when the automatic lag monitoring is turned on by passing true to the enableLagMonitor() method.

The object passed to the listener contains the following parameters:
Property Type Description
lagValue number The average of the last ten measured lag values, expressed in milliseconds.
See also
SmartFox#enableLagMonitor
constant static

PLAYER_TO_SPECTATOR  string

The playerToSpectator event type, dispatched when a player is turned to a spectator inside a Game Room.

This event is fired in response to the PlayerToSpectatorRequest> request if the operation is executed successfully.

The object passed to the listener contains the following parameters:
Property Type Description
room SFSRoom An object representing the Room in which the player is turned to spectator.
user SFSUser An object representing the player who was turned to spectator.

Example

This example turns the current user from player to spectator in the last joined Game Room.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.PLAYER_TO_SPECTATOR, onPlayerToSpectatorSwitch, this);
	sfs.addEventListener(SFS2X.SFSEvent.PLAYER_TO_SPECTATOR_ERROR, onPlayerToSpectatorSwitchError, this);

	// Switch player to spectator
	sfs.send(new SFS2X.PlayerToSpectatorRequest());
}

function onPlayerToSpectatorSwitch(evtParams)
{
	console.log("Player " + evtParams.user + " is now a spectator");
}

function onPlayerToSpectatorSwitchError(evtParams)
{
	console.log("Unable to become a spectator due to the following error: " + evtParams.errorMessage);
}
See also
PlayerToSpectatorRequest
SFSEvent.PLAYER_TO_SPECTATOR_ERROR
SFSEvent.SPECTATOR_TO_PLAYER
constant static

PLAYER_TO_SPECTATOR_ERROR  string

The playerToSpectatorError event type, dispatched when an error occurs while the current user is being turned from player to spectator in a Game Room.

This event is fired in response to the PlayerToSpectatorRequest request in case the operation failed.

The object passed to the listener contains the following parameters:
Property Type Description
errorMessage string A message containing the description of the error.
errorCode number The error code.

See the example provided in the PLAYER_TO_SPECTATOR constant description.

See also
PlayerToSpectatorRequest
SFSEvent.PLAYER_TO_SPECTATOR
constant static

PRIVATE_MESSAGE  string

The privateMessage event type, dispatched when a private message is received by the current user.

This event is caused by a PrivateMessageRequest request sent by any user in the Zone.

NOTE: the same event is fired by the sender's client too, so that the user is aware that the message was delivered successfully to the recipient, and it can be displayed in the private chat area keeping the correct message ordering. In this case there is no default way to know who the message was originally sent to. As this information can be useful in scenarios where the sender is chatting privately with more than one user at the same time in separate windows or tabs (and we need to write his own message in the proper one), the data parameter can be used to store, for example, the id of the recipient user.

The object passed to the listener contains the following parameters:
Property Type Description
sender SFSUser An object representing the user who sent the message.
message string The message sent by the user.
data SFSObject A SFSObject containing custom parameters which might accompany the message.

Example

This example sends a private message and handles the respective event.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.PRIVATE_MESSAGE, onPrivateMessage, this);

	// Send a private message to Jack
	var user = sfs.usermanager.getUserByName("Jack");
	sfs.send(new SFS2X.PrivateMessageRequest("Hello my friend!", user.id));
}

function onPrivateMessage(evtparams)
{
	// As messages are forwarded to the sender too,
	// I have to check if I am the sender

	var sender = evtParams.sender;

	if (sender != sfs.mySelf)
		console.log("User " + sender.name + " sent me this PM: " + evtParams.message);
}
See also
PrivateMessageRequest
SFSEvent.PUBLIC_MESSAGE
constant static

PROXIMITY_LIST_UPDATE  string

The proximityListUpdate event type, dispatched when one more users or one or more MMOItem objects enter/leave the current user's Area of Interest in a MMORoom.

This event is fired after an MMORoom is joined and the SetUserPositionRequest request is sent at least one time.

NOTE: this event substitutes the default userEnterRoom and userExitRoom events available in regular Rooms.

The object passed to the listener contains the following parameters:
Property Type Description
addedUsers Array of SFSUser A list of SFSUser objects representing the users who entered the current user's Area of Interest.
removedUsers Array of SFSUser A list of SFSUser objects representing the users who left the current user's Area of Interest.
addedItems Array of MMOItem A list of MMOItem objects which entered the current user's Area of Interest.
removedItems Array of MMOItem A list of MMOItem objects which left the current user's Area of Interest.

Example

This example shows how to handle the proximity user list provided by the event in order to add new avatars on the screen and remove those who left the current user's proximity range.

function onProximityListUpdate(evtParams)
{
	var added = evtParams.addedUsers;
	var removed = evtParams.removedUsers;

	// Add users that entered the proximity list
	for (var i = 0; i < added.length; i++)
	{
		var user = added[i];

		// Obtain the coordinates at which the user "appeared" in our range
		var entryPoint = user.aoiEntryPoint;

		// Add new avatar on screen
		var avatarSprite = new AvatarSprite();
		avatarSprite.x = entryPoint.px;
		avatarSprite.y = entryPoint.py;
		...
	}

	// Remove users that left the proximity list
	for (var j = 0; j < removed.length; j++)
	{
		var user = removed[j];

		// Remove the avatar from screen
		...
	}
}
See also
SetUserPositionRequest
MMORoom
constant static

PUBLIC_MESSAGE  string

The publicMessage event type, dispatched when a public message is received by the current user.

This event is caused by a PublicMessageRequest request sent by any user in the target Room, including the current user himself.

The object passed to the listener contains the following parameters:
Property Type Description
room SFSRoom An object representing the Room at which the message is targeted.
sender SFSUser An object representing the user who sent the message.
message string The message sent by the user.
data SFSObject A SFSObject containing custom parameters which might accompany the message.

Example

This example sends a public message and handles the respective event.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.PUBLIC_MESSAGE, onPublicMessage, this);

	// Send a public message
	sfs.send(new SFS2X.PublicMessageRequest("Hello everyone!"));
}

function onPublicMessage(evtParams)
{
	// As messages are forwarded to the sender too,
	// I have to check if I am the sender

	var sender = evtParams.sender;

	if (sender == sfs.mySelf)
		console.log("I said: " + evtParams.message);
	else
		console.log("User " + sender.name + " said: " + evtParams.message);
}
See also
PublicMessageRequest
SFSEvent.PRIVATE_MESSAGE
constant static

ROOM_ADD  string

The roomAdd event type, dispatched when a new Room is created inside the Zone under any of the Room Groups that the client subscribed.

This event is fired in response to the CreateRoomRequest and CreateSFSGameRequest requests in case the operation is executed successfully.

The object passed to the listener contains the following parameters:
Property Type Description
room SFSRoom An object representing the Room that was created.

Example

This example creates a new chat Room.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_ADD, onRoomCreated, this);
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_CREATION_ERROR, onRoomCreationError, this);

	// Define the settings of a new chat Room
	var settings = new SFS2X.RoomSettings("My Chat Room");
	settings.maxUsers = 40;
	settings.groupId = "chats";

	// Create the Room
	sfs.send(new SFS2X.CreateRoomRequest(settings));
}

function onRoomCreated(evtParams)
{
	console.log("Room created: " + evtParams.room);
}

function onRoomCreationError(evtParams)
{
	console.log("Room creation failure: " + evtParams.errorMessage);
}
See also
CreateRoomRequest
CreateSFSGameRequest
SFSEvent.ROOM_REMOVE
SFSEvent.ROOM_CREATION_ERROR
constant static

ROOM_CAPACITY_CHANGE  string

The roomCapacityChange event type, dispatched when the capacity of a Room is changed.

This event is fired in response to the ChangeRoomCapacityRequest> request if the operation is executed successfully.

The object passed to the listener contains the following parameters:
Property Type Description
room SFSRoom An object representing the Room whose capacity was changed.

Example

This example changes the capacity of an existing Room.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_CAPACITY_CHANGE, onRoomCapacityChanged, this);
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_CAPACITY_CHANGE_ERROR, onRoomCapacityChangeError, this);

	var theRoom = sfs.getRoomByName("Gonzo's Room");

	// Resize the Room so that it allows a maximum of 100 users and zero spectators
	sfs.send(new SFS2X.ChangeRoomCapacityRequest(theRoom, 100, 0));
}

function onRoomCapacityChanged(evtParams)
{
	console.log("The capacity of Room " + evtParams.room.name + " was changed successfully");
}

function onRoomCapacityChangeError(evtParams)
{
	console.log("Room capacity change failed: " + evtParams.errorMessage);
}
See also
ChangeRoomCapacityRequest
SFSEvent.ROOM_CAPACITY_CHANGE_ERROR
constant static

ROOM_CAPACITY_CHANGE_ERROR  string

The roomCapacityChangeError event type, dispatched when an error occurs while attempting to change the capacity of a Room.

This event is fired in response to the ChangeRoomCapacityRequest request in case the operation failed.

The object passed to the listener contains the following parameters:
Property Type Description
errorMessage string A message containing the description of the error.
errorCode number The error code.

See the example provided in the ROOM_CAPACITY_CHANGE constant description.

See also
ChangeRoomCapacityRequest
SFSEvent.ROOM_CAPACITY_CHANGE
constant static

ROOM_CREATION_ERROR  string

The roomCreationError event type, dispatched if an error occurs while creating a new Room.

This event is fired in response to the CreateRoomRequest and CreateSFSGameRequest requests in case the operation failed.

The object passed to the listener contains the following parameters:
Property Type Description
errorMessage string A message containing the description of the error.
errorCode number The error code.

See the example provided in the ROOM_ADD constant description.

See also
CreateRoomRequest
CreateSFSGameRequest
SFSEvent.ROOM_ADD
constant static

ROOM_FIND_RESULT  string

The roomFindResult event type, dispatched when a Rooms search is completed.

This event is fired in response to the FindRoomsRequest request to return the search result.

The object passed to the listener contains the following parameters:
Property Type Description
rooms Array of SFSRoom A list of SFSRoom objects representing the Rooms matching the search criteria. If no Room is found, the list is empty.

Example

This example looks for all the server Rooms whose "country" Room Variable is set to "Sweden".

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_FIND_RESULT, onRoomFindResult, this);

	// Create a matching expression to find Rooms with a "country" variable equal to "Sweden"
	var exp = new SFS2X.MatchExpression("country", SFS2X.Entities.Match.StringMatch.EQUALS, "Sweden");

	// Find the Rooms
	sfs.send(new SFS2X.FindRoomsRequest(exp));
}

function onRoomFindResult(evtParams)
{
	console.log("Rooms found: " + evtParams.rooms);
}
See also
FindRoomsRequest
MatchExpression
constant static

ROOM_GROUP_SUBSCRIBE  string

The roomGroupSubscribe event type, dispatched when a Group is subscribed by the current user.

This event is fired in response to the SubscribeRoomGroupRequest> request if the operation is executed successfully.

The object passed to the listener contains the following parameters:
Property Type Description
groupId string The name of the Group that was subscribed.
newRooms Array of SFSRoom A list of SFSRoom objects representing the Rooms belonging to the subscribed Group.

Example

This example makes the current user subscribe a Group.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_GROUP_SUBSCRIBE, onGroupSubscribed, this);
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_GROUP_SUBSCRIBE_ERROR, onGroupSubscribeError, this);

	// Subscribe the "cardGames" group
	sfs.send(new SFS2X.SubscribeRoomGroupRequest("cardGames"));
}

function onGroupSubscribed(evtParams)
{
	console.log("Group subscribed. The following rooms are now accessible: " + evtParams.newRooms);
}

function onGroupSubscribeError(evtParams)
{
	console.log("Group subscription failed: " + evtParams.errorMessage);
}
See also
SubscribeRoomGroupRequest
SFSEvent.ROOM_GROUP_SUBSCRIBE_ERROR
SFSEvent.ROOM_GROUP_UNSUBSCRIBE
constant static

ROOM_GROUP_SUBSCRIBE_ERROR  string

The roomGroupSubscribeError event type, dispatched when an error occurs while a Room Group is being subscribed.

This event is fired in response to the SubscribeRoomGroupRequest request in case the operation failed.

The object passed to the listener contains the following parameters:
Property Type Description
errorMessage string A message containing the description of the error.
errorCode number The error code.

See the example provided in the ROOM_GROUP_SUBSCRIBE constant description.

See also
SubscribeRoomGroupRequest
SFSEvent.ROOM_GROUP_SUBSCRIBE
constant static

ROOM_GROUP_UNSUBSCRIBE  string

The roomGroupUnsubscribe event type, dispatched when a Group is unsubscribed by the current user.

This event is fired in response to the UnsubscribeRoomGroupRequest request if the operation is executed successfully.

The object passed to the listener contains the following parameters:
Property Type Description
groupId string The name of the Group that was unsubscribed.

Example

This example makes the current user unsubscribe a Group.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_GROUP_UNSUBSCRIBE, onGroupUnsubscribed, this);
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_GROUP_UNSUBSCRIBE_ERROR, onGroupUnsubscribeError, this);

	// Unsubscribe the "cardGames" group
	sfs.send(new SFS2X.UnsubscribeRoomGroupRequest("cardGames"));
}

function onGroupUnsubscribed(evtParams)
{
	console.log("Group unsubscribed: " + evtParams.groupId);
}

function onGroupUnsubscribeError(evtParams)
{
	console.log("Group unsubscribing failed: " + evtParams.errorMessage);
}
See also
UnsubscribeRoomGroupRequest
SFSEvent.ROOM_GROUP_UNSUBSCRIBE_ERROR
SFSEvent.ROOM_GROUP_SUBSCRIBE
constant static

ROOM_GROUP_UNSUBSCRIBE_ERROR  string

The roomGroupUnsubscribeError event type, dispatched when an error occurs while a Room Group is being unsubscribed.

This event is fired in response to the UnsubscribeRoomGroupRequest request in case the operation failed.

The object passed to the listener contains the following parameters:
Property Type Description
errorMessage string A message containing the description of the error.
errorCode number The error code.

See the example provided in the ROOM_GROUP_UNSUBSCRIBE constant description.

See also
UnsubscribeRoomGroupRequest
SFSEvent.ROOM_GROUP_UNSUBSCRIBE
constant static

ROOM_JOIN  string

The roomJoin event type, dispatched when a Room is joined by the current user.

This event is fired in response to the JoinRoomRequest and QuickJoinGameRequest requests in case the operation is executed successfully.

The object passed to the listener contains the following parameters:
Property Type Description
room SFSRoom An object representing the Room that was joined.

Example

This example makes the user join an existing Room.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_JOIN, onRoomJoined, this);
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_JOIN_ERROR, onRoomJoinError, this);

	// Join a Room called "Lobby"
	sfs.send(new SFS2X.JoinRoomRequest("Lobby"));
}

function onRoomJoined(evtParams)
{
	console.log("Room joined successfully: " + evtParams.room);
}

function onRoomJoinError(evtParams)
{
	console.log("Room joining failed: " + evtParams.errorMessage);
}
See also
JoinRoomRequest
QuickJoinGameRequest
SFSEvent.ROOM_JOIN_ERROR
constant static

ROOM_JOIN_ERROR  string

The roomJoinError event type, dispatched when an error occurs while the current user is trying to join a Room.

This event is fired in response to the JoinRoomRequest request in case the operation failed.

The object passed to the listener contains the following parameters:
Property Type Description
errorMessage string A message containing the description of the error.
errorCode number The error code.

See the example provided in the ROOM_JOIN constant description.

See also
JoinRoomRequest
SFSEvent.ROOM_JOIN
constant static

ROOM_NAME_CHANGE  string

The roomNameChange event type, dispatched when the name of a Room is changed.

This event is fired in response to the ChangeRoomNameRequest request if the operation is executed successfully.

The object passed to the listener contains the following parameters:
Property Type Description
room SFSRoom An object representing the Room which was renamed.
oldName string The previous name of the Room.

Example

This example renames an existing Room.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_NAME_CHANGE, onRoomNameChanged, this);
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_NAME_CHANGE_ERROR, onRoomNameChangeError, this);

	var theRoom = sfs.getRoomByName("Gonzo's Room");
	sfs.send(new SFS2X.ChangeRoomNameRequest(theRoom, "Gonzo The Great's Room"));
}

function onRoomNameChanged(evtParams)
{
	console.log("Room " + evtParams.oldName + " was successfully renamed to " + evtParams.room.name);
}

function onRoomNameChangeError(evtParams)
{
	console.log("Room name change failed: " + evtParams.errorMessage);
}
See also
ChangeRoomNameRequest
SFSEvent.ROOM_NAME_CHANGE_ERROR
constant static

ROOM_NAME_CHANGE_ERROR  string

The roomNameChangeError event type, dispatched when an error occurs while attempting to change the name of a Room.

This event is fired in response to the ChangeRoomNameRequest request in case the operation failed.

The object passed to the listener contains the following parameters:
Property Type Description
errorMessage string A message containing the description of the error.
errorCode number The error code.

See the example provided in the ROOM_NAME_CHANGE constant description.

See also
ChangeRoomNameRequest
SFSEvent.ROOM_NAME_CHANGE
constant static

ROOM_PASSWORD_STATE_CHANGE  string

The roomPasswordStateChange event type, dispatched when the password of a Room is set, changed or removed.

This event is fired in response to the ChangeRoomPasswordStateRequest> request if the operation is executed successfully.

The object passed to the listener contains the following parameters:
Property Type Description
room SFSRoom An object representing the Room whose password was changed.

Example

This example changes the password of an existing Room.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_PASSWORD_STATE_CHANGE, onRoomPasswordStateChanged, this);
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_PASSWORD_STATE_CHANGE_ERROR, onRoomPasswordStateChangeError, this);

	var theRoom = sfs.getRoomByName("Gonzo's Room");
	sfs.send(new SFS2X.ChangeRoomPasswordStateRequest(theRoom, "mammamia"));
}

function onRoomPasswordStateChanged(evtParams)
{
	console.log("The password of Room " + evtParams.room.name + " was changed successfully");
}

function onRoomPasswordStateChangeError(evtParams)
{
	console.log("Room password change failed: " + evtParams.errorMessage);
}
See also
ChangeRoomPasswordStateRequest
SFSEvent.ROOM_PASSWORD_STATE_CHANGE_ERROR
constant static

ROOM_PASSWORD_STATE_CHANGE_ERROR  string

The roomPasswordStateChangeError event type, dispatched when an error occurs while attempting to set, change or remove the password of a Room.

This event is fired in response to the ChangeRoomPasswordStateRequest request in case the operation failed.

The object passed to the listener contains the following parameters:
Property Type Description
errorMessage string A message containing the description of the error.
errorCode number The error code.

See the example provided in the ROOM_PASSWORD_STATE_CHANGE constant description.

See also
ChangeRoomPasswordStateRequest
SFSEvent.ROOM_PASSWORD_STATE_CHANGE
constant static

ROOM_REMOVE  string

The roomRemove event type, dispatched when a Room belonging to one of the Groups subscribed by the client is removed from the Zone.

The object passed to the listener contains the following parameters:
Property Type Description
room SFSRoom An object representing the Room that was removed.

Example

This example shows how to handle this event type.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_REMOVE, onRoomRemoved, this);
}

function onRoomRemoved(evtParams)
{
	console.log("The following Room was removed: " + evtParams.room);
}
See also
SFSEvent.ROOM_ADD
constant static

ROOM_VARIABLES_UPDATE  string

The roomVariablesUpdate event type, dispatched when a Room Variable is updated.

This event is caused by the SetRoomVariablesRequest request. The request could have been sent by a user in the same Room of the current user or, in case of a global Room Variable, by a user in a Room belonging to one of the Groups subscribed by the current client.

The object passed to the listener contains the following parameters:
Property Type Description
room SFSRoom An object representing the Room where the Room Variable update occurred.
changedVars Array of string The list of names of the Room Variables that were changed (or created for the first time).

Example

This example sets a number of Room Variables and handles the respective update event.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.ROOM_VARIABLES_UPDATE, onRoomVarsUpdate, this);

	// Create some Room Variables
	var roomVars = [];
	roomVars.push(new SFS2X.SFSRoomVariable("gameStarted", false));
	roomVars.push(new SFS2X.SFSRoomVariable("gameType", "Snooker"));
	roomVars.push(new SFS2X.SFSRoomVariable("minRank", 10));

	sfs.send(new SFS2X.SetRoomVariablesRequest(roomVars));
}

function onRoomVarsUpdate(evtParams)
{
	var changedVars = evtParams.changedVars;
	var room = evtParams.room;

	// Check if the "gameStarted" variable was changed
	if (changedVars.indexOf("gameStarted") != -1)
	{
		if (room.getVariable("gameStarted") == true)
			console.log("Game started");
		else
			console.log("Game stopped");
	}
}
See also
SetRoomVariablesRequest
SFSRoomVariable
constant static

SOCKET_ERROR  string

The socketError event type, dispatched when a low level socket error is detected, for example bad/inconsistent data.

The object passed to the listener contains the following parameters:
Property Type Description
errorMessage string The description of the error.
constant static

SPECTATOR_TO_PLAYER  string

The spectatorToPlayer event type, dispatched when a spectator is turned to a player inside a Game Room.

This event is fired in response to the SpectatorToPlayerRequest> request if the operation is executed successfully.

The object passed to the listener contains the following parameters:
Property Type Description
room SFSRoom An object representing the Room in which the spectator is turned to player.
user SFSUser An object representing the spectator who was turned to player.
playerId number The player id of the user.

Example

This example turns the current user from spectator to player in the last joined Game Room.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.SPECTATOR_TO_PLAYER, onSpectatorToPlayerSwitch, this);
	sfs.addEventListener(SFS2X.SFSEvent.SPECTATOR_TO_PLAYER_ERROR, onSpectatorToPlayerSwitchError, this);

	// Switch spectator to player
	sfs.send(new SFS2X.SpectatorToPlayerRequest());
}

function onSpectatorToPlayerSwitch(evtParams)
{
	console.log("Spectator " + evtParams.user + " is now a player");
}

function onSpectatorToPlayerSwitchError(evtParams)
{
	console.log("Unable to become a player due to the following error: " + evtParams.errorMessage);
}
See also
SpectatorToPlayerRequest
SFSEvent.SPECTATOR_TO_PLAYER_ERROR
SFSEvent.PLAYER_TO_SPECTATOR
constant static

SPECTATOR_TO_PLAYER_ERROR  string

The spectatorToPlayerError event type, dispatched when an error occurs while the current user is being turned from spectator to player in a Game Room.

This event is fired in response to the SpectatorToPlayerRequest request in case the operation failed.

The object passed to the listener contains the following parameters:
Property Type Description
errorMessage string A message containing the description of the error.
errorCode number The error code.

See the example provided in the SPECTATOR_TO_PLAYER constant description.

See also
SpectatorToPlayerRequest
SFSEvent.SPECTATOR_TO_PLAYER
constant static

USER_COUNT_CHANGE  string

The userCountChange event type, dispatched when the number of users/players or spectators inside a Room changes.

This event is caused by a JoinRoomRequest request or a LeaveRoomRequest request. The Room must belong to one of the Groups subscribed by the current client; also this event might be fired or not depending on the Room configuration defined upon its creation (see the RoomSettings.events setting).

The object passed to the listener contains the following parameters:
Property Type Description
room SFSRoom An object representing the Room in which the users count changed.
uCount number The new users count (players in case of Game Room).
sCount number The new spectators count (Game Room only).

Example

This example shows how to handle this event type.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.USER_COUNT_CHANGE, onUserCountChange, this);
}

function onUserCountChange(evtParams)
{
	var room = evtParams.room;
	var uCount = evtParams.uCount;
	var sCount = evtParams.sCount;

	console.log("Room: " + room.name + " now contains " + uCount + " users and " + sCount + " spectators");
}
See also
JoinRoomRequest
LeaveRoomRequest
RoomSettings#events
SFSEvent.USER_ENTER_ROOM
SFSEvent.USER_EXIT_ROOM
constant static

USER_ENTER_ROOM  string

The userEnterRoom event type, dispatched when one of the Rooms joined by the current user is entered by another user.

This event is caused by a JoinRoomRequest request; it might be fired or not depending on the Room configuration defined upon its creation (see the RoomSettings.events setting).

NOTE: if the Room is of type MMORoom, this event is never fired and it is substituted by the proximityListUpdate event.

The object passed to the listener contains the following parameters:
Property Type Description
user SFSUser An object representing the user who joined the Room.
room SFSRoom An object representing the Room that was joined by a user.

Example

This example shows how to handle this event type.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.USER_ENTER_ROOM, onUserEnterRoom, this);
}

function onUserEnterRoom(evtParams)
{
	var room = evtParams.room;
	var user = evtParams.user;

	console.log("User " + user.name + " just joined Room " + room.name);
}
See also
JoinRoomRequest
RoomSettings#events
MMORoom
SFSEvent.USER_EXIT_ROOM
SFSEvent.USER_COUNT_CHANGE
SFSEvent.PROXIMITY_LIST_UPDATE
constant static

USER_EXIT_ROOM  string

The userExitRoom event type, dispatched when one of the Rooms joined by the current user is left by another user, or by the current user himself.

This event is caused by a LeaveRoomRequest request; it might be fired or not depending on the Room configuration defined upon its creation (see the RoomSettings.events setting).

NOTE: if the Room is of type MMORoom, this event is fired when the current user leaves the Room only. For the other users leaving the Room it is substituted by the proximityListUpdate event.

The object passed to the listener contains the following parameters:
Property Type Description
user SFSUser An object representing the user who left the Room.
room SFSRoom An object representing the Room that was left by a user.

Example

This example shows how to handle this event type.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.USER_EXIT_ROOM, onUserExitRoom, this);
}

function onUserExitRoom(evtParams)
{
	var room = evtParams.room;
	var user = evtParams.user;

	console.log("User " + user.name + " just left Room " + room.name);
}
See also
LeaveRoomRequest
RoomSettings#events
MMORoom
SFSEvent.USER_ENTER_ROOM
SFSEvent.USER_COUNT_CHANGE
SFSEvent.PROXIMITY_LIST_UPDATE
constant static

USER_FIND_RESULT  string

The userFindResult event type, dispatched when a users search is completed.

This event is fired in response to the FindUsersRequest request to return the search result.

The object passed to the listener contains the following parameters:
Property Type Description
users Array of SFSUser A list of SFSUser objects representing the users matching the search criteria. If no user is found, the list is empty.

Example

This example looks for all the users whose "age" User Variable is greater than 29.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.USER_FIND_RESULT, onUserFindResult, this);

	// Create a matching expression to find users with an "age" variable greater than 29:
	var exp = new SFS2X.MatchExpression("age", SFS2X.NumberMatch.GREATER_THAN, 29);

	// Find the users
	sfs.send(new SFS2X.FindUsersRequest(exp));
}

function onUserFindResult(evtParams)
{
	console.log("Users found: " + evtParams.users);
}
See also
FindUsersRequest
MatchExpression
constant static

USER_VARIABLES_UPDATE  string

The userVariablesUpdate event type, dispatched when a User Variable is updated.

This event is caused by the SetUserVariablesRequest request sent by a user in one of the Rooms joined by the current user.

The object passed to the listener contains the following parameters:
Property Type Description
user SFSUser An object representing the user who updated his own User Variables.
changedVars Array of string The list of names of the User Variables that were changed (or created for the first time).

Example

This example sets a number of User Variables and handles the respective update event.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.USER_VARIABLES_UPDATE, onUserVarsUpdate, this);

	// Create some User Variables
	var userVars = [];
	userVars.push(new SFS2X.SFSUserVariable("avatarType", "SwedishCook"));
	userVars.push(new SFS2X.SFSUserVariable("country", "Sweden"));
	userVars.push(new SFS2X.SFSUserVariable("x", 10));
	userVars.push(new SFS2X.SFSUserVariable("y", 5));

	sfs.send(new SFS2X.SetUserVariablesRequest(userVars));
}

function onUserVarsUpdate(evtParams)
{
	var changedVars = evtParams.changedVars;
	var user = evtParams.user;

	// Check if the user changed his x and y user variables
	if (changedVars.indexOf("x") != -1 || changedVars.indexOf("y") != -1)
	{
		// Move the user avatar to a new position
		...
	}
}
See also
SetUserVariablesRequest
SFSUserVariable