Basic Unity Multiplayer Game With Networker – Part 3

In this part of the tutorial we will look at creating our client.

Let’s go ahead and add a new project. This should be a .NET Standard Class Library. The reason we pick this project type is so we can test it in our console application, but later we can import it into our Unity project. Call it Tutorial.Client.

This .csproj will also need the NuGet packages which we added to the server project in the previous parts of this tutorial. Edit your .csproj to add the NuGet references.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Networker" Version="3.0.0" />
    <PackageReference Include="Networker.Extensions.ProtobufNet" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
  </ItemGroup>

</Project>

We will also need a new configuration file for the client. So in the same way we did in part 2, add a new file clientSettings.json to our client project. This configuration file looks the same as the server, except we have added a new entry for the Address.

{
  "Networker": {
    "TcpPort": 1000,
    "UdpPort": 1001,
    "Address": "localhost" 
  },
  "Logging": {
    "LogLevel": {
      "Default": "Debug"
    },
    "Console": {
      "LogLevel": {
        "Default": "Debug"
      }
    }
  }
}

We need a new class in this project called GameClient. Inside this class we must add a constructor to create our client. This is done in a similar way to the ServerBuilder but instead we are using the ClientBuilder.

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Networker.Client;
using Networker.Client.Abstractions;
using Networker.Extentions.ProtobufNet;

namespace Tutorial.Client
{
    public class GameClient
    {
        public GameClient()
        {
            var config = new ConfigurationBuilder()
                .AddJsonFile("clientSettings.json", false, true)
                .Build();

            var networkerSettings = config.GetSection("Networker");

            Client = new ClientBuilder()
                .UseIp(networkerSettings.GetValue<string>("Address"))
                .UseTcp(networkerSettings.GetValue<int>("TcpPort"))
                .UseUdp(networkerSettings.GetValue<int>("UdpPort"))
                .ConfigureLogging(loggingBuilder =>
                {
                    loggingBuilder.AddConfiguration(config.GetSection("Logging"));
                    loggingBuilder.AddConsole();
                })
                .UseProtobufNet()
                .Build();
        }

        public IClient Client { get; set; }
    }
}

Go back to Program.cs in our server application and create an instance of the client. This is just for testing and we’ll remove it later.

static void Main(string[] args)
{
    IConfiguration config = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", false, true)
        .Build();

    var networkerSettings = config.GetSection("Networker");

    var server = new ServerBuilder()
        .UseTcp(networkerSettings.GetValue<int>("TcpPort"))
        .UseUdp(networkerSettings.GetValue<int>("UdpPort"))
        .ConfigureLogging(loggingBuilder =>
        {
            loggingBuilder.AddConfiguration(config.GetSection("Logging"));
            loggingBuilder.AddConsole();
        })
        .UseProtobufNet()
        .Build();

    server.Start();

    var gameClient = new GameClient();
    gameClient.Client.Connect();

    while (server.Information.IsRunning)
    {
        Thread.Sleep(10000);
    }
}

Go ahead and run the application, you should see some more logs showing a successful connection.


Leave a comment

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