Click or drag to resize

ClusterJoinOrCreateRequest Class

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.
Inheritance Hierarchy
SystemObject
  BaseRequest
    Sfs2X.Requests.ClusterClusterJoinOrCreateRequest

Namespace:  Sfs2X.Requests.Cluster
Assembly:  SmartFox2X (in SmartFox2X.dll) Version: 1.8.0.0 (1.8.0)
Syntax
C#
public class ClusterJoinOrCreateRequest : BaseRequest
Constructors
Remarks
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 CONNECTION_REQUIRED event is dispatched to the requester's client, followed by a ROOM_JOIN event after the connection and login process is completed. In case the client is already connected to the target Game Node, the ROOM_JOIN event is received immediately.

Examples
The following example makes the user quickly join a public game:
void SomeMethod() {
    sfsLobby.AddEventListener(SFSClusterEvent.CONNECTION_REQUIRED, OnGameNodeConnectionRequired);

       // Create a matching expression to find a Darts game with a "maxBet" variable less than 100
    MatchExpression exp = new MatchExpression("type", StringMatch.EQUALS, "Darts").And("maxBet", NumberMatch.LESS_THAN, 100);

       // Set the Room settings to create a new game Room if one is not found
       ClusterRoomSettings settings = new ClusterRoomSettings("NewRoom__" + new System.Random().Next());
       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 ClusterJoinOrCreateRequest(exp, new List<string>(){"games"}, settings));
}

void OnGameNodeConnectionRequired(BaseEvent evt)
{
    // Retrieve connection settings
    ConfigData cfg = (ConfigData)evt.Params["configData"];

    // Retrieve and save login details
    gameUsername = (string)evt.Params["userName"];
    gamePassword = (string)evt.Params["password"];

    // Initialize SmartFox client used to connect to the cluster game node
    sfsGame = new SmartFox();

    // Add event listeners
    sfsGame.AddEventListener(SFSEvent.CONNECTION, OnGameNodeConnection);
    sfsGame.AddEventListener(SFSEvent.LOGIN, OnGameNodeLogin);
    sfsGame.AddEventListener(SFSEvent.ROOM_JOIN, OnGameRoomJoin);

    // Establish a connection to the game node; a game room will be joined automatically after login
    sfsGame.Connect(cfg);
}

void OnGameNodeConnection(BaseEvent evt)
{
    if ((bool)evt.Params["success"])
    {
        // Login
        sfsGame.Send(new LoginRequest(gameUsername, gamePassword));
    }
}

void OnGameNodeLogin(BaseEvent evt)
{
    // Nothing to do; a game Room-autojoin is triggered by the server
}

void OnGameRoomJoin(BaseEvent evt) {    
    Console.WriteLine("Successfully joined Room: " + (Room)evt.Params["room"]);                         // .Net / Unity
    System.Diagnostics.Debug.WriteLine("Successfully joined Room: " + (Room)evt.Params["room"]);        // UWP
}
See Also