UNPKG

silvie

Version:

Typescript Back-end Framework

50 lines (49 loc) 2.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _socket = require("socket.io"); var _flatten = _interopRequireDefault(require("../../utils/import/flatten")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } const config = process.configs.socket; class SocketServer { constructor() { _defineProperty(this, "io", void 0); _defineProperty(this, "namespaces", []); } init(HTTPServer, namespaces, instanceCallback) { let io = new _socket.Server(HTTPServer.server, { path: config.path, connectTimeout: config.connectTimeout, upgradeTimeout: config.upgradeTimeout, pingTimeout: config.pingTimeout, pingInterval: config.pingInterval, transports: config.transports, allowUpgrades: config.allowUpgrades, perMessageDeflate: config.perMessageDeflate, httpCompression: config.httpCompression, allowEIO3: config.allowEIO3, cors: config.cors, cookie: config.cookie }); if (instanceCallback instanceof Function) { io = instanceCallback(io); } this.io = io; this.registerNamespace(...(0, _flatten.default)(namespaces)); } registerNamespace(...namespaces) { this.namespaces.push(...namespaces); namespaces.forEach(namespace => { namespace.server = this; namespace.namespace = this.io.of(namespace.name); this.io.of(namespace.name).on('connection', namespace.onConnection); }); } } const server = new SocketServer(); var _default = exports.default = server;