Skip to content

Overview

To get started developing multiplayer applications with SmartFoxServer we need to learn a few simple concepts. We'll take a quick tour of the main steps in this overview, and get more in depth in the next chapters of this section.

In this introduction we'll learn the following:

  • Connecting to the server
  • Logging into a Zone
  • Interacting with the server

Connecting to the server

First we want to connect to the server using either TCP or WebSocket. This is mostly determined by what client platform you're using: Unity and Godot can talk to SmartFoxServer using both protocols, while HTML/JS clients can only use WebSocket.

Regardless, the connection to SmartFoxServer looks as simple as this:

var sfs = new SmartFox();
sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);

var cfgData = new ConfigData();
sfs.Connect(cfgData);

private void OnConnection(ApiEvent evt) {
    var success = (Boolean) evt.Param[EventParam.Success]!;

    if (success) {
        Log("Connection success");
    } else {
        Log("Connection Failed: " + evt.Params[EventParam.ErrorMessage]);
    }
}

First we instantiate a SmartFox object which is the main client API class and define a callback for the OnConnection event. This will handle the result of our connection operation.

Next we create a ConfigData object which defines a number of connection settings. For a local connection to our dev machine we don't need to customize any of the settings, but we'll learn more about them in the next chapters.

Finally we call the Connect() method and handle the result in the OnConnection callback.

Logging into a Zone

After we have established a connection we want to select a Zone and log into it. A Zone defines a unique area of the server where an application is running. When managing multiple applications you can define as many Zones as necessary.

By default SmartFoxServer has one defined Zone called BasicExamples used by all the example tutorials we provide. Continuing the previous code example, let's send a LoginRequest and join it:

var sfs = new SmartFox();
sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
sfs.AddEventListener(SFSEvent.LOGIN, OnLogin);
sfs.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);

var cfgData = new ConfigData();
cfgData.Zone = "BasicExamples";

sfs.Connect(cfgData);

private void OnConnection(ApiEvent evt) {
    var success = (Boolean) evt.Param[EventParam.Success]!;

    if (success)
    {
        Log("Connection success");
        sfs.Send(new LoginRequest("Kermit the Frog"));
    } else {
        Log("Connection Failed: " + evt.Params[EventParam.ErrorMessage]);
    }
}


private void OnLogin(ApiEvent evt) {
    Log("Logged in successfully as: " + sfs.MySelf.Name);
}


private void onLoginError(ApiEvent evt) {
    log.info("Login error: " + evt.Params[EventParam.ErrorMessage]);
}

We have added two more event listeners to handle the server's responses for the LoginRequest and we've specified the name of the Zone we want to access. Finally we send the LoginRequest with our user name, and join the Zone.

After a successful login you are ready to interact with the application in any way you want: search other Users, join or create new Rooms, send public or private messages, etc.

Interacting with the server

Once we're logged in we can interact with the server in two main ways:

  1. The System API provided by SmartFoxServer: we offer a large number of requests for managing Users, Rooms, Buddy lists, match-making, invitations, moderation and lots more.
  2. By calling custom server Extensions, which can add new behavior, custom game logic, interact with databases and external services etc.

ServerOverview

This diagram recapitulates what we have seen so far: we can access the server by starting a connection to and logging in an existing Zone. Next we can start sending Request objects to the server via the two modes just mentioned. Requests are then dispatched either to the System Controller which handles all of the standard API calls or the ExtensionController which deals with custom server requests. These in turn are routed to your custom server side code to execute the requested commands.

Examples of the former type of requests are:

  • LoginRequest, which we have already seen
  • JoinRoomRequest (joins a specific Room)
  • PrivateMessageRequest (sends a private message to a User)
  • etc...

We'll learn more about all the available requests in the next chapters of the documentation