Skip to content

Class: RoomClient<View, Command> ​

Defined in: client/index.ts:170

A connection to one room. Holds the latest state the server sent and reconnects on its own; after a reconnect the server sends the current state again, so there is nothing to replay.

Type Parameters ​

Type ParameterDefault type
Viewunknown
Commandunknown

Constructors ​

Constructor ​

ts
new RoomClient<View, Command>(url, options?): RoomClient<View, Command>;

Defined in: client/index.ts:194

Parameters ​

ParameterType
urlstring | URL
options?RoomClientOptions<View>

Returns ​

RoomClient<View, Command>

Accessors ​

state ​

Get Signature ​

ts
get state(): View | undefined;

Defined in: client/index.ts:201

The latest state the room sent: this player's view. Undefined until the first one arrives.

Returns ​

View | undefined


seq ​

Get Signature ​

ts
get seq(): number;

Defined in: client/index.ts:206

The room's change counter for state. It goes up by one with every change.

Returns ​

number


tick ​

Get Signature ​

ts
get tick(): number;

Defined in: client/index.ts:211

Simulation ticks the room has run. Stays 0 in event mode.

Returns ​

number


tickRate ​

Get Signature ​

ts
get tickRate(): number;

Defined in: client/index.ts:216

The room's simulation rate in ticks per second; 0 in event mode.

Returns ​

number


you ​

Get Signature ​

ts
get you(): string | undefined;

Defined in: client/index.ts:221

This player's id, as the server knows it. Set once connected.

Returns ​

string | undefined


players ​

Get Signature ​

ts
get players(): Player[];

Defined in: client/index.ts:226

Everyone with a seat in the room, in join order.

Returns ​

Player[]


status ​

Get Signature ​

ts
get status(): ConnectionStatus;

Defined in: client/index.ts:231

Where the connection stands.

Returns ​

ConnectionStatus


unconfirmed ​

Get Signature ​

ts
get unconfirmed(): readonly SentCommand<Command>[];

Defined in: client/index.ts:242

The commands sent with send that state does not show yet, oldest first: the room has not applied them, or has and the news is on its way. A command leaves the list in the same moment state changes to show it, or when the room refuses it. Predictor draws them over state. The array is a new one whenever it changes.

Returns ​

readonly SentCommand<Command>[]

Methods ​

on() ​

ts
on<E>(event, listener): () => void;

Defined in: client/index.ts:253

Listen for state, presence, message, status or moved, in addition to the on… options. Returns a function that stops listening, which suits UI frameworks:

useEffect(() => room.on("state", setState), [room]);

Type Parameters ​

Type Parameter
E extends keyof RoomClientEvents<View>

Parameters ​

ParameterType
eventE
listenerRoomClientEvents<View>[E]

Returns ​

() => void


send() ​

ts
send(command): Promise<void>;

Defined in: client/index.ts:283

Send a command. Resolves once the room applied it, by which time state already reflects it: at once in event mode, and with the next tick in a room that is ticking. Rejects with CommandRejected if the room refused it or the connection dropped first.

Parameters ​

ParameterType
commandCommand

Returns ​

Promise<void>


leave() ​

ts
leave(): Promise<void>;

Defined in: client/index.ts:295

Give up the seat in this room. Resolves once the room let the player go, after which it closes the connection and status becomes closed. Rejects with CommandRejected if the room made the player stay.

Returns ​

Promise<void>


close() ​

ts
close(): void;

Defined in: client/index.ts:300

Close the connection for good, keeping the seat. No reconnect follows.

Returns ​

void

MIT licence. Tickflare is an independent project and is not affiliated with Cloudflare.