MultiConnectMultiConnectDocs Website home
MultiConnectSDK › Export to .NET

7. Start the server

Replace the contents of Program.cs with Script 7.1. It works on your machine and on MultiConnectCloud, because it reads its settings from what MultiConnectCloud provides, and uses a default for each when running locally.

MyRoomController in Script 7.1 is your own room controller. Write it as the ServerBootstrap class does in Script 9.2 of the Developer Guide, without the MonoBehaviour and Awake.

Script 7.1: Program.cs

using MultiConnect;
using MultiConnect.Server;

public static class Program
{
    public static void Main()
    {
        // Set before the server starts, so the first player finds it ready.
        MatchMaking.SetRoomController(new MyRoomController());

        McDotNetServer.Start(new McDotNetServerOptions
        {
            // MultiConnectCloud provides the port. 7777 is for your machine.
            Port = McHostingEnvironment.PortOr(7777),

            // Your SDK key is provided when the deployment has an SDK App. This is for your machine.
            SdkKey = Environment.GetEnvironmentVariable("MC_SDK_KEY") ?? "my-local-key",

            // A game server when MultiConnectCloud says so, otherwise one server doing everything.
            Combined = !McHostingEnvironment.IsWorkerServer,

            // Follows the deployment's WebSocket setting.
            UseWebSocket = Environment.GetEnvironmentVariable("MC_ENABLE_WEBSOCKET") == "true",
        });

        Console.WriteLine("Server started");

        // The server runs on its own threads, so keep the program open.
        Thread.Sleep(Timeout.Infinite);
    }
}

To run a dedicated master on .NET, start a second program the same way, with Master = true in the options. It finds its game servers on MultiConnectCloud by itself, as Section 9 of the Developer Guide explains.

To test it, run the server, and point a Unity client at it as in Step 7 of the Developer Guide, using the same key. A client's key must match the server's.

dotnet run