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
This event is caused by the AdminMessageRequest request sent by a user with administration privileges.
| Name | Type | Description |
|---|---|---|
sender | SFSUser | An object representing the administrator User which sent the message. |
message | string | The text of the message. |
data | SFSObject | An object containing custom parameters accompaigning the message. |
CONNECTION
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.
| Name | Type | Description |
|---|---|---|
success | boolean | true if the connection was established successfully. |
errorMessage | string | A message describing the error in case of failed connection. |
errorCode | number | The error code. |
CONNECTION_LOST
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.
| Name | Type | Description |
|---|---|---|
disconnectionReason | string | The reason of the disconnection, among those available in the ClientDisconnectionReason class. |
CONNECTION_RESUME
CONNECTION_RETRY
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
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.
| Name | Type | Description |
|---|---|---|
cmd | string | The name of the command which identifies an action that should be executed by the client. |
room | SFSRoom | An object representing the Room which the Extension is attached to (for Room Extensions only). |
data | SFSObject | An object containing custom data sent by the Extension. |
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.
| Name | Type | Description |
|---|---|---|
invitation | SFSInvitation | An object representing the Invitation received by the client. |
INVITATION_REPLY
This event is caused by the InvitationReplyRequest sent by the invited User.
| Name | Type | Description |
|---|---|---|
invitee | SFSUser | An object representing the User which replied to the Invitation. |
reply | number | The id of the reply to the Invitation, among those available as constants in the InvitationReply class. |
data | SFSObject | An optional object containing custom parameters, for example a message describing the reason why the Invitation was refused. |
INVITATION_REPLY_ERROR
This event is fired in response to the InvitationReplyRequest if the operation fails.
| Name | Type | Description |
|---|---|---|
errorMessage | string | The message that describes the error. |
errorCode | number | The error code. |
LOGIN
This event is fired in response to the LoginRequest if the operation is successful.
| Name | Type | Description |
|---|---|---|
zoneName | string | The name of the Zone where the login occurred. |
user | SFSUser | An object representing the User which performed the login. |
data | SFSObject | An object containing custom parameters returned by the server when the Zone's custom login is active. |
LOGIN_ERROR
This event is fired in response to the LoginRequest if the operation fails.
| Name | Type | Description |
|---|---|---|
errorMessage | string | The message that describes the error. |
errorCode | number | The error code. |
LOGOUT
This event is fired in response to the LogoutRequest request.
| Name | Type | Description |
|---|---|---|
zoneName | string | The name of the Zone from which the client logged out. |
MMOITEM_VARIABLES_UPDATE
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.
| Name | Type | Description |
|---|---|---|
room | MMORoom | An object representing the MMORoom containing the MMOItem whose variables have been updated. |
mmoItem | MMOItem | An object representing the MMOItem whose variables have been updated. |
changedVars | Array.<string> | A list of names of variables that were modified or created. |
MODERATOR_MESSAGE
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.
| Name | Type | Description |
|---|---|---|
sender | SFSUser | An object representing the moderator User which sent the message. |
message | string | The text of the message. |
data | SFSObject | An object containing custom parameters accompaigning the message. |
OBJECT_MESSAGE
This event is caused by an ObjectMessageRequest sent by any User in the target Room.
| Name | Type | Description |
|---|---|---|
sender | SFSUser | An object representing the sender of the message. |
message | SFSObject | The content of the message (an object containing custom data). |
PING_PONG
This event is fired continuously when the automatic lag monitoring is active (see SmartFox#enableLagMonitor method).
| Name | Type | Description |
|---|---|---|
lagValue | LagValue | An object containing the average, minimum and maximum values of the measured lag, expressed in milliseconds. |
PLAYER_TO_SPECTATOR
This event is fired in response to the PlayerToSpectatorRequest if the operation is successful.
| Name | Type | Description |
|---|---|---|
user | SFSUser | An object representing the Room in which the Player was turned into Spectator. |
room | SFSRoom | An object representing the Player which was turned into Spectator. |
PLAYER_TO_SPECTATOR_ERROR
This event is fired in response to the PlayerToSpectatorRequest if the operation fails.
| Name | Type | Description |
|---|---|---|
errorMessage | string | The message that describes the error. |
errorCode | number | The error code. |
PRIVATE_MESSAGE
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.
| Name | Type | Description |
|---|---|---|
sender | SFSUser | An object representing the sender of the message. |
message | string | The text of the message. |
data | SFSObject | An object containing custom parameters accompaigning the message. |
PROXIMITY_LIST_UPDATE
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.
| Name | Type | Description |
|---|---|---|
room | SFSRoom | An object representing the Room where the event occurred. |
addedUsers | Array.<SFSUser> | A list of objects representing the Users which entered the client's Area of Interest. |
removedUsers | Array.<SFSUser> | A list of objects representing the Users which left the client's Area of Interest. |
addedItems | Array.<MMOItem> | A list of objects representing the MMOItems which entered the client's Area of Interest. |
removedItems | Array.<MMOItem> | A list of objects representing the MMOItems which left the client's Area of Interest. |
PUBLIC_MESSAGE
This event is caused by a PublicMessageRequest sent by any User in the target Room, including the current one.
| Name | Type | Description |
|---|---|---|
room | SFSRoom | An object representing the Room at which the message is targeted. |
sender | SFSUser | An object representing the sender of the message. |
message | string | The text of the message. |
data | SFSObject | An object containing custom parameters accompaigning the message. |
ROOM_ADD
This event is fired in response to the CreateRoomRequest and the CreateSFSGameRequest if the respective operations are successful.
| Name | Type | Description |
|---|---|---|
room | SFSRoom | An object representing the Room that was created. |
ROOM_CAPACITY_CHANGE
This event is fired in response to the ChangeRoomCapacityRequest if the operation is successful.
| Name | Type | Description |
|---|---|---|
room | SFSRoom | An object representing the Room whose capacity was changed. |
ROOM_CAPACITY_CHANGE_ERROR
This event is fired in response to the ChangeRoomCapacityRequest if the operation fails.
| Name | Type | Description |
|---|---|---|
errorMessage | string | The message that describes the error. |
errorCode | number | The error code. |
ROOM_CREATION_ERROR
| Name | Type | Description |
|---|---|---|
errorMessage | string | The message that describes the error. |
errorCode | number | The error code. |
ROOM_FIND_RESULT
This event is fired in response to the FindRoomsRequest to return the search result.
| Name | Type | Description |
|---|---|---|
roomList | Array.<SFSRoom> | A list of objects representing the Rooms matching the search criteria. If no Room was found, the list is empty. |
ROOM_GROUP_SUBSCRIBE
This event is fired in response to the SubscribeRoomGroupRequest if the operation is successful.
| Name | Type | Description |
|---|---|---|
groupId | string | The name of the subscribed Group. |
newRooms | Array.<SFSRoom> | A list of objects representing the Rooms belonging to the subscribed Group. |
ROOM_GROUP_SUBSCRIBE_ERROR
This event is fired in response to the SubscribeRoomGroupRequest if the operation fails.
| Name | Type | Description |
|---|---|---|
errorMessage | string | The message that describes the error. |
errorCode | number | The error code. |
ROOM_GROUP_UNSUBSCRIBE
This event is fired in response to the UnsubscribeRoomGroupRequest if the operation is successful.
| Name | Type | Description |
|---|---|---|
groupId | string | The name of the unsubscribed Group. |
ROOM_GROUP_UNSUBSCRIBE_ERROR
This event is fired in response to the UnsubscribeRoomGroupRequest if the operation fails.
| Name | Type | Description |
|---|---|---|
errorMessage | string | The message that describes the error. |
errorCode | number | The error code. |
ROOM_JOIN
This event is fired in response to the JoinRoomRequest and QuickJoinGameRequest if the respective operations are successful.
| Name | Type | Description |
|---|---|---|
roomAn | SFSRoom | object representing the Room that was joined. |
ROOM_JOIN_ERROR
This event is fired in response to the JoinRoomRequest if the operation fails.
| Name | Type | Description |
|---|---|---|
errorMessage | string | The message that describes the error. |
errorCode | number | The error code. |
ROOM_NAME_CHANGE
This event is fired in response to the ChangeRoomNameRequest if the operation is successful.
| Name | Type | Description |
|---|---|---|
room | SFSRoom | An object representing the Room which was renamed. |
oldName | string | The previous name of the Room. |
ROOM_NAME_CHANGE_ERROR
This event is fired in response to the ChangeRoomNameRequest if the operation fails.
| Name | Type | Description |
|---|---|---|
errorMessage | string | The message that describes the error. |
errorCode | number | The error code. |
ROOM_PASSWORD_STATE_CHANGE
This event is fired in response to the ChangeRoomPasswordStateRequest if the operation is successful.
| Name | Type | Description |
|---|---|---|
room | SFSRoom | An object representing the Room whose password state was changed. |
ROOM_PASSWORD_STATE_CHANGE_ERROR
This event is fired in response to the ChangeRoomPasswordStateRequest if the operation fails.
| Name | Type | Description |
|---|---|---|
errorMessage | string | The message that describes the error. |
errorCode | number | The error code. |
ROOM_REMOVE
| Name | Type | Description |
|---|---|---|
room | SFSRoom | An object representing the Room that was removed. |
ROOM_VARIABLES_UPDATE
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.
| Name | Type | Description |
|---|---|---|
room | SFSRoom | An object representing the Room where the Room Variable update occurred. |
changedVars | Array.<string> | A list of names of variables that were modified or created. |
SPECTATOR_TO_PLAYER
This event is fired in response to the SpectatorToPlayerRequest if the operation is successful.
| Name | Type | Description |
|---|---|---|
user | SFSUser | An object representing the Spectator which was turned into Player. |
room | SFSRoom | An object representing the Room in which the Spectator was turned into Player. |
playerId | number | The Player id of the User. |
SPECTATOR_TO_PLAYER_ERROR
This event is fired in response to the SpectatorToPlayerRequest if the operation fails.
| Name | Type | Description |
|---|---|---|
errorMessage | string | The message that describes the error. |
errorCode | number | The error code. |
USER_COUNT_CHANGE
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).
| Name | Type | Description |
|---|---|---|
room | SFSRoom | An object representing the Room in which the User count changed. |
userCount | number | The new User count (Player count in case of Game Room). |
specCount | number | The new Spectator count (Game Rooms only). |
USER_ENTER_ROOM
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.
| Name | Type | Description |
|---|---|---|
user | SFSUser | An object representing the User which joined the Room. |
room | SFSRoom | An object representing the Room that was joined by a User. |
USER_EXIT_ROOM
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.
| Name | Type | Description |
|---|---|---|
user | SFSUser | An object representing the User which left the Room. |
room | SFSRoom | An object representing the Room that was left by a User. |
USER_FIND_RESULT
This event is fired in response to the FindUsersRequest to return the search result.
| Name | Type | Description |
|---|---|---|
userList | Array.<SFSUser> | A list of objects representing the Users matching the search criteria. If no User was found, the list is empty. |
USER_VARIABLES_UPDATE
This event is caused by the SetUserVariablesRequest sent by a User in one of the Rooms joined by the client.
| Name | Type | Description |
|---|---|---|
user | SFSUser | An object representing the User which updated its own User Variables. |
changedVars | Array.<string> | A list of names of variables that were modified or created. |