SFSClusterEvent
The Cluster related event types dispatched by the SmartFoxServer 2X JavaScript API.
Properties
new SFSClusterEvent()
Developers never istantiate the SFSClusterEvent 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 approach to be implemented to listen to events; please refer to the specific event types for the parameters object content.
function someMethod()
{
sfsLobby.addEventListener(SFS2X.SFSClusterEvent.CONNECTION_REQUIRED, onGameNodeConnectionRequired, this);
sfsLobby.addEventListener(SFS2X.SFSClusterEvent.LOAD_BALANCER_ERROR, onLoadBalancerError, this);
// Set request parameters
...
// Send request
sfsLobby.send(new SFS2X.ClusterJoinOrCreateRequest(exp, group, settings));
}
function onGameNodeConnectionRequired(evtParams)
{
// Retrieve login details for later usage
gameUsername = evtParams.userName;
gamePassword = evtParams.password;
// Initialize SmartFox client used to connect to the cluster game node
sfsGame = new SFS2X.SmartFox();
// Add event listeners
...
// Establish a connection to the game node; a game room will be joined automatically after login
sfsGame.connect(evtParams.configObj);
}
function onLoadBalancerError(evtParams)
{
console.log("A load balancer error occurred: you should investigate the cluster state");
}
Properties
CONNECTION_REQUIRED string
The connectionRequired event type, dispatched when the Lobby has found a Game Node for the client to join in a SmartFoxServer 2X Cluster.
This event is fired when the Lobby signals that the client should join a given Game Node, in response to a ClusterJoinOrCreateRequest request or when an invitation to join an existing game on a Game Node is accepted by the invited user.
Property | Type | Description |
---|---|---|
configObj | object | The pre-populated client configuration object to start a new connection towards the designated Game Node. |
userName | string | The user name to access the Game Node. |
password | string | A temporary and unique password to access the Game Node. |
Example
This example shows how to attempt a connection to a Game Node when the event is received.
function someMethod()
{
sfsLobby.addEventListener(SFS2X.SFSClusterEvent.CONNECTION_REQUIRED, onGameNodeConnectionRequired, this);
}
function onGameNodeConnectionRequired(evtParams)
{
// Retrieve connection settings
var config = evtParams.configObj;
// Retrieve login details for later usage
gameUsername = evtParams.userName;
gamePassword = evtParams.password;
// Initialize SmartFox client used to connect to the cluster game node
sfsGame = new SFS2X.SmartFox();
// Add event listeners
sfsGame.addEventListener(SFS2X.SFSEvent.CONNECTION, onGameNodeConnection, this);
sfsGame.addEventListener(SFS2X.SFSEvent.LOGIN, onGameNodeLogin, this);
sfsGame.addEventListener(SFS2X.SFSEvent.ROOM_JOIN, onGameRoomJoin, this);
// Establish a connection to the game node; a game room will be joined automatically after login
sfsGame.connect(config);
}
LOAD_BALANCER_ERROR string
The loadBalancerError event type, dispatched when a cluster-related request cannot be satisfied, typically creating or joining a game in a SmartFoxServer 2X Cluster.
For example, this event can be fired in response to the ClusterJoinOrCreateRequest request.
The event does not provide further details as the Load Balancer simply queries the available servers and if none is found matching the contextual criteria, the cluster request cannot be completed.
Example
This example shows how to handle this event type.
function someMethod()
{
sfsLobby.addEventListener(SFS2X.SFSClusterEvent.LOAD_BALANCER_ERROR, onLoadBalancerError, this);
}
function onLoadBalancerError(evtParams)
{
console.log("A load balancer error occurred: you should investigate the cluster state");
}