UNPKG

gamesocket.io

Version:
77 lines (76 loc) 2.92 kB
'use strict' var __importDefault = (this && this.__importDefault) || function (mod) { return mod && mod.__esModule ? mod : { default: mod } } Object.defineProperty(exports, '__esModule', { value: true }) const uuid_1 = require('uuid') const ServerProxy_js_1 = require('../ServerProxy/ServerProxy.js') const Logger_js_1 = __importDefault(require('../Logger/Logger.js')) const controller = function (namespace, ...destinations) { if ((0, uuid_1.validate)(destinations[0])) return new SocketDestination(namespace, destinations) else return new RoomDestination(namespace, destinations) } class SocketDestination { _name _sockets constructor(_name, _sockets) { this._name = _name this._sockets = _sockets _sockets.filter((id) => ServerProxy_js_1.ServerProxy.has(id)) } emit(event, ...data) { for (let id of this._sockets.values()) ServerProxy_js_1.ServerProxy.send(id, event, ...data) } join(rooms) { if (typeof rooms != 'string') { for (let id of this._sockets.values()) { rooms.forEach((room, index) => (rooms[index] = getCorrectPath(room, this._name))) for (let room of rooms) ServerProxy_js_1.ServerProxy.subscribe(id, room) } } else for (let id of this._sockets.values()) ServerProxy_js_1.ServerProxy.subscribe(id, rooms) } leave(rooms) { if (typeof rooms != 'string') { for (let id of this._sockets.values()) { rooms.forEach((room, index) => (rooms[index] = getCorrectPath(room, this._name))) for (let room of rooms) ServerProxy_js_1.ServerProxy.unsubscribe(id, room) } } else for (let id of this._sockets.values()) ServerProxy_js_1.ServerProxy.unsubscribe(id, rooms) } } class RoomDestination { _rooms constructor(name, _rooms) { this._rooms = _rooms _rooms.forEach((room, index) => { _rooms[index] = getCorrectPath(name, room) }) } emit(event, ...data) { for (let room of this._rooms) ServerProxy_js_1.ServerProxy.emit(room, event, ...data) } join(socket) { if (typeof socket != 'string') { for (let room of this._rooms) { for (let id of socket) ServerProxy_js_1.ServerProxy.subscribe(id, room) } } else for (let room of this._rooms) ServerProxy_js_1.ServerProxy.subscribe(socket, room) } leave(socket) { if (typeof socket != 'string') { for (let room of this._rooms) { for (let id of socket) ServerProxy_js_1.ServerProxy.unsubscribe(id, room) } } else for (let room of this._rooms) ServerProxy_js_1.ServerProxy.unsubscribe(socket, room) } } function getCorrectPath(name, path) { if (path == 'broadcast') Logger_js_1.default.fatal(`{ ${name}.control('broadcast') } is reserved. Try to use { ${name}.emit(...) instead }`) else if (path == 'reserved/broadcast') return `${name}/broadcast` if (path.startsWith(`${name}/`)) return path return `${name}/${path}` } exports.default = controller