UNPKG

@colyseus/core

Version:

Multiplayer Framework for Node.js.

92 lines (91 loc) 2.87 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var RelayRoom_exports = {}; __export(RelayRoom_exports, { RelayRoom: () => RelayRoom }); module.exports = __toCommonJS(RelayRoom_exports); var import_schema = require("@colyseus/schema"); var import_Room = require("../Room.js"); class Player extends import_schema.Schema { } (0, import_schema.defineTypes)(Player, { connected: "boolean", name: "string", sessionId: "string" }); class State extends import_schema.Schema { constructor() { super(...arguments); // tslint:disable-line this.players = new import_schema.MapSchema(); } } (0, import_schema.defineTypes)(State, { players: { map: Player } }); class RelayRoom extends import_Room.Room { constructor() { super(...arguments); // tslint:disable-line this.allowReconnectionTime = 0; } onCreate(options) { this.setState(new State()); if (options.maxClients) { this.maxClients = options.maxClients; } if (options.allowReconnectionTime) { this.allowReconnectionTime = Math.min(options.allowReconnectionTime, 40); } if (options.metadata) { this.setMetadata(options.metadata); } this.onMessage("*", (client, type, message) => { this.broadcast(type, [client.sessionId, message], { except: client }); }); } onJoin(client, options = {}) { const player = new Player(); player.connected = true; player.sessionId = client.sessionId; if (options.name) { player.name = options.name; } this.state.players.set(client.sessionId, player); } async onLeave(client, consented) { if (this.allowReconnectionTime > 0) { const player = this.state.players.get(client.sessionId); player.connected = false; try { if (consented) { throw new Error("consented leave"); } await this.allowReconnection(client, this.allowReconnectionTime); player.connected = true; } catch (e) { this.state.players.delete(client.sessionId); } } } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { RelayRoom });