couch-kit
    Preparing search index...

    Function useGameClient

    • React hook that connects the web controller to the TV host via WebSocket.

      Manages the full lifecycle: connection, JOIN handshake, session recovery, optimistic state updates, server time synchronization, and automatic reconnection with exponential backoff.

      Type Parameters

      Parameters

      • config: ClientConfig<S, A>

        Client configuration including reducer, initial state, and connection options.

      Returns {
          disconnect: () => void;
          getServerTime: () => number;
          playerId: string | null;
          reconnect: () => void;
          rtt: number;
          sendAction: (action: A) => void;
          state: S;
          status: "connecting" | "connected" | "disconnected" | "error";
      }

      An object with status, state, playerId, sendAction, getServerTime, rtt, disconnect, and reconnect.

      • disconnect: () => void

        Manually disconnect from the host. Prevents automatic reconnection.

      • getServerTime: () => number
      • playerId: string | null
      • reconnect: () => void

        Manually reconnect to the host. Resets the reconnection attempt counter.

      • rtt: number

        Round-trip time (ms) to the server. Updated periodically via PING/PONG.

      • sendAction: (action: A) => void
      • state: S
      • status: "connecting" | "connected" | "disconnected" | "error"
      const { state, sendAction } = useGameClient({
      reducer: gameReducer,
      initialState,
      });