SFSEvent

Represents all main event types dispatched by the SmartFoxServer Client API.


Constants contained in this object (see Events below) are used to register event listeners; when an event is dispatched, an object containing event-specific parameters is passed to the listener. Parameters are described under each event type; to avoid typing errors, get parameter names from the EventParam class.

Example

This example shows the generic approach to follow to listen to events.

var sfs = null;

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

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

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

// Handle connection event
function onConnection(evtParams)
{
	const success = evtParams[EventParam.Success];
	
	if (success)
		console.log("Connected to SmartFoxServer");
	else
		console.log("Connection failed; is the server running at all?");
}

Events

ADMIN_MESSAGE

Event type dispatched when the client receives a message from an administrator User.

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

Properties
NameTypeDescription
senderSFSUserAn object representing the administrator User which sent the message.
messagestringThe text of the message.
dataSFSObjectAn object containing custom parameters accompaigning the message.

CONNECTION

Event type dispatched when a connection between the client and a SmartFoxServer instance is attempted.

This event is fired in response to a call to the SmartFox#connect method. The connection attempt can fail if the server is unreachable for some reason, or if an error occurs during the handshake process. In this case an error message is provided.

Properties
NameTypeDescription
successbooleantrue if the connection was established successfully.
errorMessagestringA message describing the error in case of failed connection.
errorCodenumberThe error code.

CONNECTION_LOST

Event type dispatched when the connection between the client and the SmartFoxServer instance is interrupted.

This event is fired in response to a call to the SmartFox#disconnect method, or when the connection between the client and the server is interrupted for other reasons.

Properties
NameTypeDescription
disconnectionReasonstringThe reason of the disconnection, among those available in the ClientDisconnectionReason class.

CONNECTION_RESUME

Event type dispatched when the SmartFoxServer HRC System is active in the currently joined Zone and the connection between the client and the SmartFoxServer instance is re-established.

CONNECTION_RETRY

Event type dispatched when the SmartFoxServer HRC System is active in the currently joined Zone and the connection between the client and the SmartFoxServer instance is interrupted abruptly.

The HRC System allows a broken connection to be re-established transparently within a certain amount of time, without losing any of the current application state. For example, this allows any player to get back to a game without losing the match because of a sloppy internet connection.

When this event is dispatched the API enter a "freeze" mode during which no new requests can be sent until the reconnection is successfully performed. It is highly recommended to handle this event and freeze the application interface accordingly until the SFSEvent.CONNECTION_RESUME event is fired, or the reconnection fails, the client is definitely disconnected and the SFSEvent.CONNECTION_LOST event is fired.

EXTENSION_RESPONSE

Event type dispatched when data coming from a server-side Extension is received by the client.

Data is usually sent by the server to one or more clients in response to an ExtensionRequest, but not necessarily. If this event is fired in response to a request sent by the client, it is common practice to use the same value of the cmd parameter passed to the request also in the response.

Properties
NameTypeDescription
cmdstringThe name of the command which identifies an action that should be executed by the client.
roomSFSRoomAn object representing the Room which the Extension is attached to (for Room Extensions only).
dataSFSObjectAn object containing custom data sent by the Extension.

INVITATION

Event type dispatched when the client receives an Invitation.

This event is caused by the InviteUsersRequest and the client is supposed to reply by means of the InvitationReplyRequest.

The event can also be fired when a CreateSFSGameRequest is sent and its settings cause Invitations to join the Game Room to be dispatched.

Properties
NameTypeDescription
invitationSFSInvitationAn object representing the Invitation received by the client.

INVITATION_REPLY

Event type dispatched when the client receives a reply to a previously sent Invitation.

This event is caused by the InvitationReplyRequest sent by the invited User.

Properties
NameTypeDescription
inviteeSFSUserAn object representing the User which replied to the Invitation.
replynumberThe id of the reply to the Invitation, among those available as constants in the InvitationReply class.
dataSFSObjectAn optional object containing custom parameters, for example a message describing the reason why the Invitation was refused.

INVITATION_REPLY_ERROR

Event type dispatched when an error occurs while the client is sending a reply to an Invitation.

This event is fired in response to the InvitationReplyRequest if the operation fails.

Properties
NameTypeDescription
errorMessagestringThe message that describes the error.
errorCodenumberThe error code.

LOGIN

Event type dispatched when the client performs a successful login in a server Zone.

This event is fired in response to the LoginRequest if the operation is successful.

Properties
NameTypeDescription
zoneNamestringThe name of the Zone where the login occurred.
userSFSUserAn object representing the User which performed the login.
dataSFSObjectAn object containing custom parameters returned by the server when the Zone's custom login is active.

LOGIN_ERROR

Event type dispatched when an error occurs during the login process.

This event is fired in response to the LoginRequest if the operation fails.

Properties
NameTypeDescription
errorMessagestringThe message that describes the error.
errorCodenumberThe error code.

LOGOUT

Event type dispatched when the client logs out from the server Zone.

This event is fired in response to the LogoutRequest request.

Properties
NameTypeDescription
zoneNamestringThe name of the Zone from which the client logged out.

MMOITEM_VARIABLES_UPDATE

Event type dispatched when one or more MMOItem Variables are created or updated.

This event is caused by MMOItem Variable/s being set, updated or deleted in a server-side Extension. It is received by the client if the relative MMOItem is inside its Area of Interest only.

Properties
NameTypeDescription
roomMMORoomAn object representing the MMORoom containing the MMOItem whose variables have been updated.
mmoItemMMOItemAn object representing the MMOItem whose variables have been updated.
changedVarsArray.<string>A list of names of variables that were modified or created.

MODERATOR_MESSAGE

Event type dispatched when the client receives a message from a moderator User.

This event can be caused by the ModeratorMessageRequest, the KickUserRequest or the BanUserRequest sent by a User with at least moderation privileges. Also, this event can be caused by a kick/ban action executed in the SmartFoxServer Administration Tool.

Properties
NameTypeDescription
senderSFSUserAn object representing the moderator User which sent the message.
messagestringThe text of the message.
dataSFSObjectAn object containing custom parameters accompaigning the message.

OBJECT_MESSAGE

Event type dispatched when an object containing custom data is received by the client.

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

Properties
NameTypeDescription
senderSFSUserAn object representing the sender of the message.
messageSFSObjectThe content of the message (an object containing custom data).

PING_PONG

Event type dispatched when a new lag value is measured.

This event is fired continuously when the automatic lag monitoring is active (see SmartFox#enableLagMonitor method).

Properties
NameTypeDescription
lagValueLagValueAn object containing the average, minimum and maximum values of the measured lag, expressed in milliseconds.

PLAYER_TO_SPECTATOR

Event type dispatched when a Player is turned into Spectator inside a Game Room.

This event is fired in response to the PlayerToSpectatorRequest if the operation is successful.

Properties
NameTypeDescription
userSFSUserAn object representing the Room in which the Player was turned into Spectator.
roomSFSRoomAn object representing the Player which was turned into Spectator.

PLAYER_TO_SPECTATOR_ERROR

Event type dispatched when an error occurs while the client is being turned from Player to Spectator in a Game Room.

This event is fired in response to the PlayerToSpectatorRequest if the operation fails.

Properties
NameTypeDescription
errorMessagestringThe message that describes the error.
errorCodenumberThe error code.

PRIVATE_MESSAGE

Event type dispatched when a private message is received by the client.

This event is caused by a PrivateMessageRequest sent by any User in the logged Zone.

The same event is fired by the sender's client too, so that the application 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 the recipient to which the message was originally sent. 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 modals or tabs (and we need to write its own message in the proper one), the data parameter can be used to store, for example, the id of the recipient User.

Properties
NameTypeDescription
senderSFSUserAn object representing the sender of the message.
messagestringThe text of the message.
dataSFSObjectAn object containing custom parameters accompaigning the message.

PROXIMITY_LIST_UPDATE

Event type dispatched when one or more Users or MMOItems enter/leave the client's Area of Interest in a MMORoom.

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

Note that this event substitutes the default SFSEvent.USER_ENTER_ROOM and SFSEvent.USER_EXIT_ROOM events available in regular Rooms.

Properties
NameTypeDescription
roomSFSRoomAn object representing the Room where the event occurred.
addedUsersArray.<SFSUser>A list of objects representing the Users which entered the client's Area of Interest.
removedUsersArray.<SFSUser>A list of objects representing the Users which left the client's Area of Interest.
addedItemsArray.<MMOItem>A list of objects representing the MMOItems which entered the client's Area of Interest.
removedItemsArray.<MMOItem>A list of objects representing the MMOItems which left the client's Area of Interest.

PUBLIC_MESSAGE

Event type dispatched when a public message is received by the client.

This event is caused by a PublicMessageRequest sent by any User in the target Room, including the current one.

Properties
NameTypeDescription
roomSFSRoomAn object representing the Room at which the message is targeted.
senderSFSUserAn object representing the sender of the message.
messagestringThe text of the message.
dataSFSObjectAn object containing custom parameters accompaigning the message.

ROOM_ADD

Event type dispatched when a new Room is created inside the logged Zone under any of the Room Groups that the client subscribed.

This event is fired in response to the CreateRoomRequest and the CreateSFSGameRequest if the respective operations are successful.

Properties
NameTypeDescription
roomSFSRoomAn object representing the Room that was created.

ROOM_CAPACITY_CHANGE

Event type dispatched when the capacity of a Room in the currently joined Zone is changed.

This event is fired in response to the ChangeRoomCapacityRequest if the operation is successful.

Properties
NameTypeDescription
roomSFSRoomAn object representing the Room whose capacity was changed.

ROOM_CAPACITY_CHANGE_ERROR

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 if the operation fails.

Properties
NameTypeDescription
errorMessagestringThe message that describes the error.
errorCodenumberThe error code.

ROOM_CREATION_ERROR

Event type dispatched if an error occurs while creating a new Room. This event is fired in response to the CreateRoomRequest and the CreateSFSGameRequest if the operation fails.
Properties
NameTypeDescription
errorMessagestringThe message that describes the error.
errorCodenumberThe error code.

ROOM_FIND_RESULT

Event type dispatched when a Room search is executed.

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

Properties
NameTypeDescription
roomListArray.<SFSRoom>A list of objects representing the Rooms matching the search criteria. If no Room was found, the list is empty.

ROOM_GROUP_SUBSCRIBE

Event type dispatched when a Group is subscribed by the client.

This event is fired in response to the SubscribeRoomGroupRequest if the operation is successful.

Properties
NameTypeDescription
groupIdstringThe name of the subscribed Group.
newRoomsArray.<SFSRoom>A list of objects representing the Rooms belonging to the subscribed Group.

ROOM_GROUP_SUBSCRIBE_ERROR

Event type dispatched when an error occurs while a Room Group is being subscribed.

This event is fired in response to the SubscribeRoomGroupRequest if the operation fails.

Properties
NameTypeDescription
errorMessagestringThe message that describes the error.
errorCodenumberThe error code.

ROOM_GROUP_UNSUBSCRIBE

Event type dispatched when a Group is unsubscribed by the client.

This event is fired in response to the UnsubscribeRoomGroupRequest if the operation is successful.

Properties
NameTypeDescription
groupIdstringThe name of the unsubscribed Group.

ROOM_GROUP_UNSUBSCRIBE_ERROR

Event type dispatched when an error occurs while a Room Group is being unsubscribed.

This event is fired in response to the UnsubscribeRoomGroupRequest if the operation fails.

Properties
NameTypeDescription
errorMessagestringThe message that describes the error.
errorCodenumberThe error code.

ROOM_JOIN

Event type dispatched when a Room is joined by the client.

This event is fired in response to the JoinRoomRequest and QuickJoinGameRequest if the respective operations are successful.

Properties
NameTypeDescription
roomAnSFSRoomobject representing the Room that was joined.

ROOM_JOIN_ERROR

Event type dispatched when an error occurs while the client is trying to join a Room.

This event is fired in response to the JoinRoomRequest if the operation fails.

Properties
NameTypeDescription
errorMessagestringThe message that describes the error.
errorCodenumberThe error code.

ROOM_NAME_CHANGE

Event type dispatched when the name of a Room in the currently joined Zone is changed.

This event is fired in response to the ChangeRoomNameRequest if the operation is successful.

Properties
NameTypeDescription
roomSFSRoomAn object representing the Room which was renamed.
oldNamestringThe previous name of the Room.

ROOM_NAME_CHANGE_ERROR

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 if the operation fails.

Properties
NameTypeDescription
errorMessagestringThe message that describes the error.
errorCodenumberThe error code.

ROOM_PASSWORD_STATE_CHANGE

Event type dispatched when the password of a Room in the currently joined Zone is set, changed or removed.

This event is fired in response to the ChangeRoomPasswordStateRequest if the operation is successful.

Properties
NameTypeDescription
roomSFSRoomAn object representing the Room whose password state was changed.

ROOM_PASSWORD_STATE_CHANGE_ERROR

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 if the operation fails.

Properties
NameTypeDescription
errorMessagestringThe message that describes the error.
errorCodenumberThe error code.

ROOM_REMOVE

Event type dispatched when a Room belonging to one of the Groups subscribed by the client is removed from the logged Zone.
Properties
NameTypeDescription
roomSFSRoomAn object representing the Room that was removed.

ROOM_VARIABLES_UPDATE

Event type dispatched when one or more Room Variables are created or updated.

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

Properties
NameTypeDescription
roomSFSRoomAn object representing the Room where the Room Variable update occurred.
changedVarsArray.<string>A list of names of variables that were modified or created.

SPECTATOR_TO_PLAYER

Event type dispatched when a Spectator is turned into Player inside a Game Room.

This event is fired in response to the SpectatorToPlayerRequest if the operation is successful.

Properties
NameTypeDescription
userSFSUserAn object representing the Spectator which was turned into Player.
roomSFSRoomAn object representing the Room in which the Spectator was turned into Player.
playerIdnumberThe Player id of the User.

SPECTATOR_TO_PLAYER_ERROR

Event type dispatched when an error occurs while the client is being turned from Spectator to Player in a Game Room.

This event is fired in response to the SpectatorToPlayerRequest if the operation fails.

Properties
NameTypeDescription
errorMessagestringThe message that describes the error.
errorCodenumberThe error code.

USER_COUNT_CHANGE

Event type dispatched when the number of Users/Players or Spectators inside a Room changes.

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

Properties
NameTypeDescription
roomSFSRoomAn object representing the Room in which the User count changed.
userCountnumberThe new User count (Player count in case of Game Room).
specCountnumberThe new Spectator count (Game Rooms only).

USER_ENTER_ROOM

Event type dispatched when one of the Rooms joined by the client is entered by another User.

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

Note that if the Room is of type MMORoom, this event type is never fired and it is substituted by the SFSEvent.PROXIMITY_LIST_UPDATE event type.

Properties
NameTypeDescription
userSFSUserAn object representing the User which joined the Room.
roomSFSRoomAn object representing the Room that was joined by a User.

USER_EXIT_ROOM

Event type dispatched when one of the Rooms joined by the client is left by a User, including the current one.

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

Note that if the Room is of type MMORoom, this event type is never fired and it is substituted by the SFSEvent.PROXIMITY_LIST_UPDATE event type.

Properties
NameTypeDescription
userSFSUserAn object representing the User which left the Room.
roomSFSRoomAn object representing the Room that was left by a User.

USER_FIND_RESULT

Event type dispatched when a User search is executed.

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

Properties
NameTypeDescription
userListArray.<SFSUser>A list of objects representing the Users matching the search criteria. If no User was found, the list is empty.

USER_VARIABLES_UPDATE

Event type dispatched when one or more User Variables are created or updated.

This event is caused by the SetUserVariablesRequest sent by a User in one of the Rooms joined by the client.

Properties
NameTypeDescription
userSFSUserAn object representing the User which updated its own User Variables.
changedVarsArray.<string>A list of names of variables that were modified or created.