@utsp/runtime-server
Version:
Server-side runtime for UTSP applications (Node.js, headless)
144 lines (139 loc) • 3.21 kB
TypeScript
export { IApplication } from '@utsp/types';
/**
* Server Runtime Options
*/
interface ServerRuntimeOptions {
/**
* The application implementation
*/
application: any;
/**
* Server port
*/
port: number;
/**
* Server host
* @default '0.0.0.0'
*/
host?: string;
/**
* Server tick rate (updates per second)
* @default 60
*/
tickRate?: number;
/**
* Maximum concurrent connections
* @default 100
*/
maxConnections?: number;
/**
* Terminal width (columns)
* @default 80
*/
width?: number;
/**
* Terminal height (rows)
* @default 25
*/
height?: number;
/**
* CORS configuration
* @default { origin: '*' }
*/
cors?: {
origin: string | string[];
credentials?: boolean;
};
/**
* Enable debug logging
* @default false
*/
debug?: boolean;
}
/**
* Server Runtime Statistics
*/
interface ServerRuntimeStats {
/** Runtime mode */
mode: 'server';
/** Whether the runtime is running */
running: boolean;
/** Number of connected users */
userCount: number;
/** Current tick rate (TPS) */
tps: number;
/** Uptime in milliseconds */
uptime: number;
/** Total ticks processed */
totalTicks: number;
}
/**
* Server Runtime
*
* Headless mode - runs server without rendering.
* Uses: Core + Network (SocketIOServer)
* No Render, no Input (receives input from clients)
*
* Perfect for:
* - Multiplayer game servers
* - Authoritative game logic
* - Scalable backend
*/
declare class ServerRuntime {
private core;
private network;
private options;
private running;
private startTime;
private lastTimestamp;
private tickCount;
private tps;
private tpsUpdateTime;
private tickInterval;
constructor(options: ServerRuntimeOptions);
getMode(): 'server';
isRunning(): boolean;
/**
* Change le tick rate du serveur (peut être appelé à tout moment)
* Redémarre l'interval avec la nouvelle fréquence
*
* @param tickRate - Nouveau tick rate en TPS (ticks per second)
* @example
* ```typescript
* // Dans init() de votre application :
* init(core: UTSPCore, runtime: ServerRuntime): void {
* runtime.setTickRate(60); // Passer à 60 TPS
* }
* ```
*/
setTickRate(tickRate: number): void;
/**
* Récupère le tick rate actuel
*/
getTickRate(): number;
start(): Promise<void>;
stop(): Promise<void>;
getStats(): ServerRuntimeStats;
destroy(): Promise<void>;
/**
* Setup network event handlers
*/
private setupNetworkHandlers;
/**
* Main tick loop
*/
private tick;
/**
* Broadcast updates to all connected clients
* Uses split packets:
* - Static layers → Reliable channel (guaranteed delivery)
* - Dynamic layers → Volatile channel (can drop packets for performance)
*/
private broadcastUpdates;
/**
* Debug logging
*/
private log;
}
export { ServerRuntime };
export type { ServerRuntimeOptions, ServerRuntimeStats };