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.
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 Parameter | Default type |
|---|---|
D | unknown |
Constructors ​
Constructor ​
new LobbyHost<D>(options?): LobbyHost<D>;Defined in: src/net/lobby/LobbyHost.ts:109
Parameters ​
| Parameter | Type |
|---|---|
options | LobbyHostOptions<D> |
Returns ​
LobbyHost<D>
Properties ​
matchmaker ​
readonly matchmaker: Matchmaker;Defined in: src/net/lobby/LobbyHost.ts:97
rooms ​
readonly rooms: LobbyRooms;Defined in: src/net/lobby/LobbyHost.ts:96
Methods ​
connect() ​
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 ​
| Parameter | Type |
|---|---|
socket | HostSocket |
Returns ​
number
dataOf() ​
dataOf(id): D | undefined;Defined in: src/net/lobby/LobbyHost.ts:156
A player's public data (what admit returned).
Parameters ​
| Parameter | Type |
|---|---|
id | number |
Returns ​
D | undefined
disconnect() ​
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 ​
| Parameter | Type |
|---|---|
id | number |
Returns ​
void
finish() ​
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 ​
| Parameter | Type |
|---|---|
matchId | string |
Returns ​
void
message() ​
message(id, data): void;Defined in: src/net/lobby/LobbyHost.ts:135
A message arrived from socket id.
Parameters ​
| Parameter | Type |
|---|---|
id | number |
data | string | ArrayBuffer | Uint8Array<ArrayBufferLike> |
Returns ​
void
send() ​
send(id, msg): void;Defined in: src/net/lobby/LobbyHost.ts:161
Sends one of the game's own messages to a player.
Parameters ​
| Parameter | Type |
|---|---|
id | number |
msg | | LobbyServerMessage | { [k: string]: unknown; t: string; } |
Returns ​
void
start() ​
start(): void;Defined in: src/net/lobby/LobbyHost.ts:166
Searches the queue and runs the ready checks on a timer.
Returns ​
void
stop() ​
stop(): void;Defined in: src/net/lobby/LobbyHost.ts:171
Returns ​
void
update() ​
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 ​
| Parameter | Type |
|---|---|
now | number |
Returns ​
void