Skip to content

1681-engine / index / LobbyHost

Class: LobbyHost<D> ​

Defined in: src/net/lobby/LobbyHost.ts:95

The front room of an online game, transport-free like RoomHost: players say hello and browse rooms; a room has a host, a code to share, ready flags, chat and the game's own settings; a player alone or a whole room queues for a mode, the Matchmaker finds matches by rating, and each match goes through a ready check before onStart turns it into a game. Someone who declines (or lets the check lapse) is locked out of the queue for a while; everyone else goes back to their old place in it.

ts
const lobby = new LobbyHost({
  modes: [{ id: 'duel', teams: 2, teamSize: 1 }, { id: '2v2', teams: 2, teamSize: 2 }],
  admit: (d) => ({ name: String((d as { name?: unknown })?.name ?? 'player').slice(0, 16) }),
  ratingOf: (id) => ratings.get(id) ?? 1000,
  onStart: (match) => ({ server: 'wss://eu1.example.com', ticket: issueTicket(match) }),
});
wss.on('connection', (ws) => {
  const id = lobby.connect(ws);
  ws.on('message', (data, binary) => lobby.message(id, binary ? '' : data.toString()));
  ws.on('close', () => lobby.disconnect(id));
});
lobby.start();

Type Parameters ​

Type ParameterDefault type
Dunknown

Constructors ​

Constructor ​

ts
new LobbyHost<D>(options?): LobbyHost<D>;

Defined in: src/net/lobby/LobbyHost.ts:109

Parameters ​

ParameterType
optionsLobbyHostOptions<D>

Returns ​

LobbyHost<D>

Properties ​

matchmaker ​

ts
readonly matchmaker: Matchmaker;

Defined in: src/net/lobby/LobbyHost.ts:97


rooms ​

ts
readonly rooms: LobbyRooms;

Defined in: src/net/lobby/LobbyHost.ts:96

Methods ​

connect() ​

ts
connect(socket): number;

Defined in: src/net/lobby/LobbyHost.ts:116

A socket connected; returns the id its messages and its close are reported under.

Parameters ​

ParameterType
socketHostSocket

Returns ​

number


dataOf() ​

ts
dataOf(id): D | undefined;

Defined in: src/net/lobby/LobbyHost.ts:156

A player's public data (what admit returned).

Parameters ​

ParameterType
idnumber

Returns ​

D | undefined


disconnect() ​

ts
disconnect(id): void;

Defined in: src/net/lobby/LobbyHost.ts:123

Socket id closed: out of its room, the queue and any match being accepted.

Parameters ​

ParameterType
idnumber

Returns ​

void


finish() ​

ts
finish(matchId): void;

Defined in: src/net/lobby/LobbyHost.ts:184

A started match is over: its rooms open again, their members not ready.

Parameters ​

ParameterType
matchIdstring

Returns ​

void


message() ​

ts
message(id, data): void;

Defined in: src/net/lobby/LobbyHost.ts:135

A message arrived from socket id.

Parameters ​

ParameterType
idnumber
datastring | ArrayBuffer | Uint8Array<ArrayBufferLike>

Returns ​

void


send() ​

ts
send(id, msg): void;

Defined in: src/net/lobby/LobbyHost.ts:161

Sends one of the game's own messages to a player.

Parameters ​

ParameterType
idnumber
msg| LobbyServerMessage | { [k: string]: unknown; t: string; }

Returns ​

void


start() ​

ts
start(): void;

Defined in: src/net/lobby/LobbyHost.ts:166

Searches the queue and runs the ready checks on a timer.

Returns ​

void


stop() ​

ts
stop(): void;

Defined in: src/net/lobby/LobbyHost.ts:171

Returns ​

void


update() ​

ts
update(now?): void;

Defined in: src/net/lobby/LobbyHost.ts:177

One round: lapsed ready checks cancelled, the queue searched, the room list pushed if it changed.

Parameters ​

ParameterType
nownumber

Returns ​

void

Released under the MIT License.