Skip to main content
Multiplayer

Not translated yet — showing English.

C# Game Servers

A C# game server runs your rules on PlayServ: each room ticks, your code steps the simulation, and the SDK pushes snapshots to the players. PlayServ verifies every player and hands out the seats; your server decides what a player may do and what happens. It's a .NET process built on PlayServ's C# SDK, deployed like a cloud function.

YoursPlayServ's
The rules: what a player may do, what it changes, who winsWho the player is: every connection arrives with a verified player
The clock and the simulationSeats: capacity, lifetime, idle timeout, max rooms, from the room settings
What players send and what you send backFinding rooms: browsing, hosting, joining and the ticket that lets a player in

Get started

Create the project

playserv init sky-arena --kind game_server

The project is an echo server you can deploy as it is. Replace its server class with your rooms, as below.

Deploy it

playserv functions deploy --slug sky-arena --kind game_server --src .

The slug is unique across your organisation, not only your project.

Set the room settings

playserv rooms config set sky-arena --capacity 8 --reservation-ttl 10 --lifetime 1800 --idle-timeout 120 --max-rooms 50

Or in Servers → Room settings. Until they exist, players can't host, browse or join its rooms.


The server class

One class per project, derived from PlatformGameServer, with a public constructor without parameters. Program.cs starts it: await PlatformHost.RunAsync<ArenaServer>(args);

HookWhen
OnStartupAsync · OnShutdownAsyncThe process starts, or is asked to stop.
OnPlayerConnected(session)A player passed the handshake. session.Id is the verified player, session.AdmittedRoomName the room their ticket is for.
OnPlayerMessage(session, bytes)One message from that player.
OnPlayerDisconnected(session, reason)The connection closed, or the reconnect grace ran out.
OnPlayerReconnected(session)The player came back within ReconnectGrace, 30 seconds by default.
OnTicketOfferAsync(room, playerId, params, ct)PlayServ asks whether this player may join this room, before it issues the ticket. Return false to refuse, for example for a private room.

Send to a player with session.SendTextAsync, SendBinaryAsync or TrySendText. Each player has a queue of 64 messages; a player too slow to keep up is disconnected rather than stalling the room.


Rooms

using PlayServ.Sdk;
using PlayServ.Sdk.Rooms;

public sealed class Pilot : RoomPlayer { public int Score; }
public sealed record ArenaConfig(int RoundSeconds);

public sealed class ArenaRoom : Room<Pilot, ArenaConfig>
{
    public ArenaRoom(string name) : base(name, new ArenaConfig(300), capacity: 8) { }

    protected override float SimStepSeconds => 0.1f;    // 10 steps per second

    protected override void OnSimStep(float dt)
    {
        // move ships, resolve hits, end the round
    }
}

A RoomHost owns the rooms of the server: new RoomHost<ArenaRoom, Pilot, ArenaConfig>(name => new ArenaRoom(name), tickHz: 10). It creates a room when a player hosts one, ticks every room, applies the room settings, sends each room's heartbeat to PlayServ about every 5 seconds, and disposes the rooms PlayServ closes. Open a room yourself with GetOrCreate(name); find one with Find(name).

Room memberWhat it does
AddPlayer · RemovePlayer · GetPlayer · Players · IsFullThe roster. AddPlayer returns false when the room is full.
LockEverything that reads or changes room state runs under it. The SDK takes it for each tick; take it yourself for each command.
OnPlayerJoined · OnPlayerLeft · OnIdleRoster hooks. OnIdle fires whenever the room is disposed: idle timeout, lifetime, or an operator's Delete room.
SetState · SetAttributesWhat players see and filter on when they browse.
MarkActive(player)Records activity, for the idle timeout.
DeclareConnect(new RoomConnect(host, port, transport), region)Where players connect, and where the room runs.

The capacity in the constructor is only for local runs: the room settings' capacity replaces it.


The simulation

OnSimStep(dt) is your fixed-step simulation. The host calls it under the room's lock, every SimStepSeconds (1/30 by default), and catches up after a stall with at most MaxCatchUpSteps steps per tick (6 by default).

Split a large simulation into systems: AddSystem(ISimSystem) and AddPlayerSystem(IPlayerSimSystem), each in a phase: Input, AI, Movement, Collision, Combat, Spawn, Scoring, Cleanup. The default OnSimStep runs them in that order.

Don't override OnTick

The room's tick steps the simulation, keeps the reconnect grace and sends snapshots. Overriding OnTick without calling base.OnTick turns all three off. Put your logic in OnSimStep.

DeclareReconnect(lingerSeconds, removeOnExpiry, onExpire) removes every player who has had no activity, recorded with MarkActive, for lingerSeconds, connected or not. So a dropped player keeps their seat for that long. onExpire runs first; with removeOnExpiry, on by default, the player is then removed and OnPlayerLeft runs. Recompute the room's host, teams or turn order there. Call MarkActive on every command a player sends, or active players are removed too.


Snapshots

Players see the room through snapshots you build; the SDK decides when to send them. Declare the plan once, in the room's constructor:

DeclareSnapshot<ArenaSnapshot>(plan =>
{
    plan.Cadence = SnapshotCadence.EveryN(20);   // every 20 ticks
    plan.Deliver = snapshot => Broadcast(snapshot);
    plan.Assemble(_ => BuildSnapshot());
});
Plan settingWhat it does
Assemble(...)Builds the snapshot after a tick, under the lock. Required.
DeliverSends it to the room's players.
CadenceSnapshotCadence.EveryTick or EveryN(n) ticks.
ForcePublishOnEventOn by default: Events.Emit(...) from any change sends a snapshot on the next tick, without waiting for the cadence.
AdaptiveCadenceSends more or less often between MinHz (1) and MaxHz (15), within a budget.

Set Deliver to send snapshots to your players' connections. A low cadence plus Events.Emit on each change makes a turn-based game feel instant.


Commands from players

What players send in OnPlayerMessage is yours to define: a small JSON message with a request ID lets you answer every command. Handle each one under the room's lock, and answer with a short error code instead of throwing.

Trust the session, not the message

Take the player from session.Id, never from an ID in the message. Check every command against your rules: the right phase, the right player, in bounds.

A server can also expose methods players call with PlayServRpc.InvokeAsync.


Data

A deployed server reads and writes your data without a key: Platform.Table<T>(), Platform.Records and Platform.Singleton. Save a match result once per match, update a player's profile by its natural key, and re-read live settings, such as a singleton, on a timer.

Never wait for a data call inside OnSimStep or under the room's lock: run it off the tick. DeclareDataMirror and DeclarePersistedCounter keep players' rows in step with the room for you.


Where players connect

Players reach the room through RoomConnect: on a machine pool or your own machines, call DeclareConnect with the public address and port. A player connects with CreateRoomSession or CreateGameConnection, which send the ticket; the SDK checks it before OnPlayerConnected, and a bad or expired ticket closes the connection.


Limits

WhatLimit
Message from a player1 MB
HandshakeThe first message, up to 4 KB, within 10 seconds
Queue per player64 messages
tickHz1–240
Catch-up6 steps per tick
Rooms and seatsFrom the room settings

Next steps

Last updated on 22 Oct 2026