UNPKG

dlovely-websocket

Version:

WebSocket For Dlovely

120 lines (119 loc) 4.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SocketApp = void 0; const events_1 = require("events"); const net_1 = __importDefault(require("net")); const tls_1 = __importDefault(require("tls")); const common_1 = require("./common"); const SocketConnect_1 = require("./SocketConnect"); const SocketControl_1 = require("./SocketControl"); class SocketApp extends events_1.EventEmitter { socket; constructor(secure, options) { super(); options?.debugger && common_1.toast.open(); const onConnection = (socket) => { let token = (0, common_1.randomString)(16); while (SocketControl_1.socketControl.isRoles(token, 'default')) { token = (0, common_1.randomString)(16); } const conn = new SocketConnect_1.AppConnection(token, socket, this, () => { common_1.toast.log(`接收到连接:`, token); SocketControl_1.socketControl.setRoles(conn, 'default'); this.emit('connection', token, conn); }); conn.on('close', () => common_1.toast.warn(`连接关闭:`, token)); conn.on('error', err => common_1.toast.error(`连接错误,已自动断开:`, token, '\r\n', err)); }; this.socket = secure && options ? tls_1.default.createServer(options, onConnection) : net_1.default.createServer(options, onConnection); this.socket.on('close', () => this.emit('close')); this.socket.on('error', err => this.emit('error', err)); this.on('connection', (token, conn) => { conn.on('text', str => { try { const { sign, data } = JSON.parse(str); common_1.toast.log(`连接[${token}]传入:${sign}${data ? typeof data === 'string' ? `\t${data}` : Object.entries(data) .map(val => `\r\n${val[0]}:\t|${val[1]}`) .join('') : ''}`); const cbs = this.signs[sign] || this.signs.unknow; if (!cbs || !(cbs instanceof Set)) return; const iterator = cbs.values(); const next = () => { const { done, value } = iterator.next(); if (done === false) value(data, conn, next); }; next(); } catch (e) { common_1.toast.log(`连接[${token}]传入:noJSON\t${str}`); const cbs = this.signs.noJSON; if (!cbs) return; const iterator = cbs.values(); const next = () => { const { done, value } = iterator.next(); if (done === false) value(str, conn, next); }; next(); } }); }); if (!options) { } else if ('selectProtocol' in options) { this._selectProtocol = options.selectProtocol; } else if ('validProtocols' in options) { this._selectProtocol = (_, protocols) => { for (const protocol in protocols) { if (options.validProtocols.indexOf(protocol) !== -1) return protocol; } return undefined; }; } } _selectProtocol; listen(port, host, callback) { if (typeof host === 'function') { callback = host; host = undefined; } if (callback) this.on('listening', callback); this.socket.listen(port, host, () => { this.emit('listening'); common_1.toast.log(`开始监听${host ? `${host}:` : ''}${port}`); }); return this; } close(callback) { this.once('close', () => common_1.toast.warn('连接关闭')); if (callback) this.once('close', callback); this.socket.close(); } signs = {}; sign(sign, ...cbs) { if (sign in this.signs) cbs.forEach(cb => this.signs[sign].add(cb)); else this.signs[sign] = new Set(cbs); return this; } control = SocketControl_1.socketControl; } exports.SocketApp = SocketApp;