Integrating Networking

// Config: Engine - [], Toolkit - [v1.0.0]

DemoNetwork is the only class the demo asks you to implement.

public class MyNetwork : DemoNetwork
{
    public override bool IsRunning => /* your library's running state */;

    public override string StartHost()
    {
        // Start your server and the host's own client. Return the address clients connect to,
        // for example "203.0.113.5:7777", or null if the server did not start.
    }

    public override bool StartClient(string address)
    {
        // Parse the address and connect. Return false if the connection could not begin.
    }

    public override void Stop()
    {
        // Shut the client and server down.
    }
}

Replace PlaceholderNetwork on the Discord Social object in the scene with your class. The address is an opaque string to Discord, so use whatever your library wants (host and port, a relay join code, a Steam id).

The session #

DemoSessionStage spawns one capsule for every lobby member and drives only the local one. To replicate positions, spawn your library’s networked player where the stage marks NETWORKING, owned by that member, and let the library move it on the other machines.

Who may connect #

Take the check from DemoWhitelist. In your library’s connection approval or first message handler, look up the Discord user id the client claims and refuse the connection when DemoWhitelist.IsAllowed(id) is false. The whitelist is the set of lobby members at the moment the host pressed Start. A client that was never in the lobby cannot be on it.

Discord does not give the server a ticket that proves who a client is. The claimed id is trusted only as far as the connection is trusted. For a game that needs more, have each client write a random value into its own lobby member metadata when it joins, send that value with the claimed id, and have the server compare it with the member’s metadata.

Reachability #

Discord has no relay. The address the host announces has to be reachable by the other players: the same LAN, a VPN such as Tailscale or ZeroTier, or a forwarded port. If your library has its own relay, announce its join code instead of an IP address.

Rate This Article!