gamesocket.io
Version:
Simple event-oriented API for uWebSocket.js
57 lines (56 loc) • 2.01 kB
JavaScript
var __importDefault =
(this && this.__importDefault) ||
function (mod) {
return mod && mod.__esModule ? mod : { default: mod }
}
Object.defineProperty(exports, '__esModule', { value: true })
exports.ServerProxy = void 0
const Logger_js_1 = __importDefault(require('../Logger/Logger.js'))
class ServerProxy {
static _sockets
static _app
static emit(room, event, ...data) {
if (Logger_js_1.default.flags.debug)
Logger_js_1.default.debug(`\t Trying to send as room '${room}' event ${event}. \n\tData: ${JSON.stringify(data)}`)
this._app.publish(room, JSON.stringify({ event: event, used: data }), true, true)
}
static send(id, event, ...data) {
if (Logger_js_1.default.flags.debug)
Logger_js_1.default.debug(`\t Trying to send as socket#${id} event ${event}. \n\tData: ${JSON.stringify(data)}`)
const socket = this.get(id)
if (socket) socket.send(JSON.stringify({ event: event, used: data }), true, true)
else if (Logger_js_1.default.flags.warn)
Logger_js_1.default.warn(`Called socket#${id} doesn't exist. Got event: '${event}'`)
}
static get(id) {
return this._sockets.get(id)
}
static has(id) {
return this._sockets.has(id)
}
static subscribe(id, room) {
if (Logger_js_1.default.flags.debug) Logger_js_1.default.debug(`\tsocket#${id} joined room ${room}`)
let socket = this.get(id)
return socket ? socket.subscribe(room) : false
}
static unsubscribe(id, room) {
if (Logger_js_1.default.flags.debug) Logger_js_1.default.debug(`\tsocket#${id} leaved room ${room}`)
let socket = this.get(id)
return socket ? socket.unsubscribe(room) : false
}
static set pool(newPool) {
this._sockets = newPool
}
static get pool() {
return this._sockets
}
static set app(newApp) {
if (Logger_js_1.default.flags.debug) Logger_js_1.default.debug(`\tApp setted to ${newApp}`)
this._app = newApp
}
static get app() {
return this._app
}
}
exports.ServerProxy = ServerProxy