QuickJoinOrCreateRoomRequest
Quickly joins the current user in a public Room, or creates a new Room if none is found.
new QuickJoinOrCreateRoomRequest(matchExpression, groupList, settings[, roomToLeave])
Creates a new QuickJoinOrCreateRoomRequest instance. The instance must be passed to the SmartFox.send() method for the request to be executed.
SmartFoxServer searches for a public Room that meets the criteria expressed by the passed matching expression in the passed Room Groups.
If no suitable Room can be found, a new Room is created, based on the passed settings. If a Room is created, the roomAdd event is dispatched to all the users who subscribed the Group to which the Room is associated, including the Room creator. In any case, if a Room to join could be found or was created, the roomJoin event is then dispatched.
Error conditions (Room creation error, Room join error) should always be checked adding the appropriate listeners.
Example
This example makes the current user quickly join a Darts game with a "maximum bet" condition.
function someMethod()
{
sfs.addEventListener(SFS2X.SFSEvent.ROOM_ADD, onRoomAdd, this);
sfs.addEventListener(SFS2X.SFSEvent.ROOM_JOIN, onRoomJoin, 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 Room if a matching one is not found
var settings = new SFS2X.RoomSettings(sfs.mySelf.name + "'s game");
settings.groupId = "games";
settings.isPublic = true;
settings.isGame = true;
settings.maxUsers = 10;
settings.minPlayersToStartGame = 2;
// Set requirements to allow users find the Room (see match expression above) in Room Variables
var roomVars = [];
roomVars.push(new SFS2X.SFSRoomVariable("type", "Darts"));
roomVars.push(new SFS2X.SFSRoomVariable("maxBet", 50));
settings.variables = roomVars;
// Search (or create) and join a public Room within the "games" Group, leaving the last joined Room
sfs.send(new SFS2X.QuickJoinOrCreateRoomRequest(exp, ["games"], settings, sfs.lastJoinedRoom));
}
function onRoomAdd(evtParams)
{
console.log("Successfully created Room: " + evtParams.room);
}
function onRoomJoin(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 Room where to join the current user. |
groupList |
|
|
An array of Group names to which the matching expression should be applied. |
settings |
|
|
An object containing the Room configuration settings. |
roomToLeave |
|
Yes |
A SFSRoom object representing the Room that the user should leave when joining the game. Defaults to |