ClusterJoinOrCreateRequest
In SmartFoxServer 2X Cluster environment, quickly joins the current user in a public game on a Game Node, or creates a new game if none is found.
new ClusterJoinOrCreateRequest(matchExpression, groupList, settings)
Creates a new ClusterJoinOrCreateRequest instance. The instance must be passed to the SmartFox.send() method for the request to be executed.
The Lobby Node searches for a public Game Room that meets the criteria expressed by the passed matching expression in the passed Room Groups. If no suitable Game Room can be found or a null matching expression is passed, and if the settings parameter is set, a new Game Room is created on a Game Node selected by the cluster's load balancing system.
In any case, if a game to join can be found or is created, the connectionRequired event is dispatched to the requester's client, followed by a roomJoin event after the connection and login process is completed. In case the client is already connected to the target Game Node, the roomJoin event is received immediately.
Example
The following example makes the user quickly join a public game.
function someMethod()
{
sfsLobby.addEventListener(SFS2X.SFSClusterEvent.CONNECTION_REQUIRED, onGameNodeConnectionRequired, this);
// Create a matching expression to find a Darts game with a "maxBet" variable less than 100
var exp = new SFS2X.MatchExpression("type", SFS2X.StringMatch.EQUALS, "darts").and("maxBet", SFS2X.NumberMatch.LESS_THAN, 100);
// Set the Room settings to create a new game Room if one is not found
var settings = new SFS2X.ClusterRoomSettings("NewRoom__" + Math.floor(Math.random() * 1000));
settings.groupId = "games";
settings.isPublic = true;
settings.isGame = true;
settings.maxUsers = 10;
settings.minPlayersToStartGame = 2;
// Search (or create) and join a public game within the "games" Group
sfsLobby.send(new SFS2X.ClusterJoinOrCreateRequest(exp, ["games"], settings));
}
function onGameNodeConnectionRequired(evtParams)
{
// Retrieve connection settings
var cfg = evtParams.configData;
// Retrieve and save login details
gameUsername = evtParams.userName;
gamePassword = evtParams.password;
// Initialize SmartFox client used to connect to the cluster game node
sfsGame = new SmartFox(cfg);
// 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();
}
function onGameNodeConnection(evtParams)
{
if (evtParams.success)
{
// Login
sfsGame.send(new SFS2X.LoginRequest(gameUsername, gamePassword));
}
}
function onGameNodeLogin(evtParams)
{
// Nothing to do; a game Room-autojoin is triggered by the server
}
function onGameRoomJoin(evtParams)
{
console.log("Successfully joined Room: " + evtParams.room);
}
Parameters
Name | Type | Optional | Description |
---|---|---|---|
matchExpression |
|
|
A matching expression that the system will use to search a Game Room where to join the current user; if Defaults to |
groupList |
|
|
A list of group names to further filter the search; if Defaults to |
settings |
|
|
If no Rooms are found through the matching expression, a new Room with the passed settings will be created and the user will auto-join it. Defaults to |