DarkRift 2 Tutorial for Unity 3D – Part 4 – Create Unity embedded server

Reminder : You can find all the DarkRift2 related articles here 
You can find the entire project on my official GitHub

Welcome back ! At this point of the tutorial, we are ready to create the unity embedded server. As explained on the last article, we need to create a specific scene for the server. Let’s create the scene named “MainServerScene” :

Create the Unity server

Open the scene

As the scene is created only to host the server process, we don’t need any camera. So delete it. Create a new GameObject in the hierarchy, and name it “GameServerManager“.

Once created, add the “Server” component provided by DarkRift. See the screenshot below :

Be careful of using the right server component, don’t use the Server (Legacy) wich is an old version of the DarkRift server component

Oh, you can see that there is few properties. I will explain them right now !

XML Unity Server properties

  • Configuration : It’s a text asset that the server needs and wich contains the configuration of the server. We’ll take a look on it later.
  • Create on enable : Says to the component that the server creates automatically on startup.
  • Events from dispatcher : DarkRift2 as an internal dispatcher (see more here). Enabling this option allow the server to receive events from the dispacther.
Good to know : The server component is named XML Unity Server only because it is excpecting a configuration file on the XML format !

The configuration file

The configuration file is XML formatted and has the following structure :

<configuration>
  <server/>
  <pluginSearch></pluginSearch>
  <logging></logging>
  <plugins ></plugins>
  <data/>
  <databases></databases>
  <listeners></listeners>
  <cache></cache>
</configuration>

As you can see, there is a lot of elements. All are mandatory, except one : <cache>. – See this for reference documentation.

Are you ready to create the configuration file ? No ? Uhhh, ok, by chance, DarkRift provides an example configuration file within the unity package.

You can find it in the following location : DarkRift/DarkRift/Plugins/Server. Drag the file to the component on the GameServerManager :

Now, try to launch the scene, and take a look to the console :

That’s all, the server is mounted and it’s ready to handle client connections and broadcast messages. That’s good but it’s not enough. we need a server manager that will handle clients connections, message broadcasting, ..

So, create folder Scripts/Network and add a new C# script named “GameServerManager“. Add it as component to the GameServerManager game object

The GameServerManager component

For now, we just want that the GameServerManager keep a list of all connected clients. for that we will use a C# List and listen to an event from the XML Unity Server called ClientConnected.

Open the script on Visual Studio or your favorite IDE… wich i hope is Visual Studio 🙂 and copy this code :

/// <summary>
/// List of connected clients
/// </summary>
public List<int> clientsId;

This list need to be initialized before to be used, so in the Start() function, write this :

    private void Start()
    {
        //////////////////
        /// Properties initialization
        clientsId = new List<int>();
    }

Now the list is ready to be used, we want to fill it with all the new client who will connect. First thing is to create a property and then get the reference of the XML Unity Server.

using DarkRift.Server.Unity;

...

    /// <summary>
    /// Reference to the DarkRift server
    /// </summary>
    public XmlUnityServer serverReference;

Don’t forget to reference the DarkRift.Server.Unity namespace ! Then, in the Start() method, use the GetComponent() function :

serverReference = GetComponent<XmlUnityServer>();

Now we have the reference to the DarkRift server, we’ll just suscribe to the event in order to add the client ID to the list. So in the Start() method :

//////////////////
/// Events subscription
serverReference.Server.ClientManager.ClientConnected += ClientConnected;

Finally, create now the ClientConnected callback function :

#region Server events
/// <summary>
/// When a client connects to the DarkRift server
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ClientConnected(object sender, ClientConnectedEventArgs e)
{
    clientsId.Add(e.Client.ID);
}
#endregion

Ok, that’s all. You now know when a client is connected and what is its ID. Good job !

Client disconnection

You can reproduce the same behavior when a client disconnect. Here is the step you need to follow :

  • Suscribe to the ClientDisconnect event
  • Create the ClientDisconnect callback function
  • Remove the ID from the clientsId list

Here is the code

private void Start()
{
    //////////////////
    /// Properties initialization
    clientsId = new List<int>();
    serverReference = GetComponent<XmlUnityServer>();

    //////////////////
    /// Events subscription
    serverReference.Server.ClientManager.ClientConnected += ClientConnected;
    serverReference.Server.ClientManager.ClientDisconnected += ClientDisconnected;
}

...

/// <summary>
/// When a client disconnects to the DarkRift server
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ClientDisconnected(object sender, ClientDisconnectedEventArgs e)
{
    throw new NotImplementedException();
}
#endregion

I have something to say you…

In fact, you can have at any time the list of all connected clients by using the following code :

//Returns all clients
serverReference.Server.ClientManager.GetAllClients();
//Return the first client connected
serverReference.Server.ClientManager.GetClient(0);

So, yes, our code is a litte useless now you are aware (JCVD) that you can access these information natively. But later in the tutorial, we’ll use this events to perform additional actions.

In the next article, we’ll load dynamically the MainGameScene and we’ll make a server build of our “game”. Bye !

Posted in Dark Rift 2 and tagged , .

One Comment

  1. Hey, I’m following your tutorial to learn darkrift, just want to point out that we need to include « using DarkRift.Server » in addition to « using DarkRift.Server.Unity » otherwise the line;

    « serverReference.Server.ClientManager.ClientConnected += ClientConnected; »

    will not work as ClientConnected is derived from DarkRift.Server. Just wanted to point that out to anyone else following this and had the same problem as me.

    Ps. the function ClientConnected is also from DarkRift.Server which is what gave it away for me.

Leave a Reply

Your email address will not be published. Required fields are marked *