Unity (C#) SDK — Realtime

Realtime

Dual-channel live multiplayer gameplay — reliable WebSocket (TCP) plus an optional UDP fast lane for movement

Constructor

public Realtime(string realtimeWebSocketUrl = "wss://realtime.michitai.com")

Initializes realtime WebSocket client with optional custom server URL. Default: wss://realtime.michitai.com

GetTokenAsync (Static)

public static async Task<TokenResponse> GetTokenAsync(
    Client client, 
    string playerToken
)

Static method to generate realtime authentication token using API client.

ConnectAsync

public async Task<bool> ConnectAsync(string realtimeToken)

Connects to WebSocket server using realtime token. Returns true if successful, false if failed. Includes connection state protection.

SendAsync

public async Task SendAsync<T>(
    ERoomTargetPlayer target,
    string command,
    T data = null,
    int[] targetIds = null
)

Sends a message to specific targets (Host, All, Others, or specific player IDs) with optional data. Uses JsonUtility for serialization.

ConnectUdpAsync

public async Task<bool> ConnectUdpAsync(int? udpPort = null)

Binds one UDP endpoint alongside the WebSocket (max 1 TCP + 1 UDP per player). Call after ConnectAsync — datagrams authenticate with the server-issued connection id. Not supported on WebGL; returns false there.

SendUnreliableAsync

public async Task SendUnreliableAsync<T>(
    ERoomTargetPlayer target,
    string command,
    T data = null,
    int[] targetIds = null
)

Sends over the bound UDP channel — unordered, unacknowledged, latest-wins via seq. Ideal for 20-60Hz movement/state streams. Falls back to the WebSocket when UDP isn't bound.

Prediction helpers

public sealed class PredictionBuffer<TState, TInput> {
    uint NextSeq(); void Record(uint seq, TInput input, TState state);
    TState Reconcile(uint ackSeq, TState authoritative,
        Func<TState,TInput,float,TState> apply, float dt);
}
public sealed class SnapshotBuffer<TState> {
    void Add(double t, TState s, uint seq = 0);
    bool TrySample(double renderTime,
        Func<TState,TState,double,TState> lerp, out TState result);
}

Client-side movement prediction: record each input with its seq, adopt authoritative state on arrival, replay unacked inputs. SnapshotBuffer interpolates remote entities ~100ms in the past. Both in Multiplayer/Rooms/Realtime/Prediction.cs.

Properties

public string ConnectionId { get; }
public bool IsUdpConnected { get; }
public int LastUdpRttMs { get; }

ConnectionId: the session id (also authenticates UDP datagrams). IsUdpConnected: UDP binding state. LastUdpRttMs: measured datagram round-trip time.

DisconnectAsync

public async Task DisconnectAsync()

Disconnects from the server and cleans up both channels. Automatically called in OnDestroy.

Events

public event Action<string, string, SenderInfo> OnReceive;
public event Action OnConnected;

OnConnected: Fired when WebSocket connection is established. OnReceive: Fired when a message is received from server with command, data, and sender information.