mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-20 01:50:21 +00:00
move lobby websockets to worker (#2974)
## Description: Currently only the master process sends public lobby updates to clients. This is not scalable since it could overload the master process. In this PR, the master uses IPC to send public lobby info to all workers. Then clients connect to a random worker to get public lobby updates via websocket. This way clients never connect directly to the master websocket. The flow looks like this: Every 100ms: 1. Master schedules a public game on a random worker if new games are needed 2. Master broadcasts public lobby info to all workers (all public games & num clients connected to each game) 3. Each worker responds to that update with the number of clients connected to its own public games 4. Master then updates its public lobby state so it knows how many clients are connected to each public game ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: evan
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { z } from "zod";
|
||||
import {
|
||||
GameConfigSchema,
|
||||
PublicGameInfoSchema,
|
||||
PublicGamesSchema,
|
||||
} from "../core/Schemas";
|
||||
|
||||
export type WorkerLobbyList = z.infer<typeof WorkerLobbyListSchema>;
|
||||
export type WorkerReady = z.infer<typeof WorkerReadySchema>;
|
||||
export type MasterLobbiesBroadcast = z.infer<
|
||||
typeof MasterLobbiesBroadcastSchema
|
||||
>;
|
||||
export type MasterCreateGame = z.infer<typeof MasterCreateGameSchema>;
|
||||
export type WorkerMessage = z.infer<typeof WorkerMessageSchema>;
|
||||
export type MasterMessage = z.infer<typeof MasterMessageSchema>;
|
||||
|
||||
// --- Worker Messages ---
|
||||
|
||||
// Worker tells the master about its lobbies.
|
||||
const WorkerLobbyListSchema = z.object({
|
||||
type: z.literal("lobbyList"),
|
||||
lobbies: z.array(PublicGameInfoSchema),
|
||||
});
|
||||
|
||||
const WorkerReadySchema = z.object({
|
||||
type: z.literal("workerReady"),
|
||||
workerId: z.number(),
|
||||
});
|
||||
|
||||
export const WorkerMessageSchema = z.discriminatedUnion("type", [
|
||||
WorkerLobbyListSchema,
|
||||
WorkerReadySchema,
|
||||
]);
|
||||
|
||||
// --- Master Messages ---
|
||||
|
||||
// Broadcasts all public game info to all workers.
|
||||
// Workers need information on all public lobbies so
|
||||
// it can send it to the client.
|
||||
const MasterLobbiesBroadcastSchema = z.object({
|
||||
type: z.literal("lobbiesBroadcast"),
|
||||
publicGames: PublicGamesSchema,
|
||||
});
|
||||
|
||||
// Master sends a message to worker to schedule a new public game/lobby.
|
||||
const MasterCreateGameSchema = z.object({
|
||||
type: z.literal("createGame"),
|
||||
gameID: z.string(),
|
||||
gameConfig: GameConfigSchema,
|
||||
startsAt: z.number(),
|
||||
});
|
||||
|
||||
export const MasterMessageSchema = z.discriminatedUnion("type", [
|
||||
MasterLobbiesBroadcastSchema,
|
||||
MasterCreateGameSchema,
|
||||
]);
|
||||
Reference in New Issue
Block a user