UNPKG

rjweb-server

Version:

Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS

118 lines (117 loc) 3.79 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Base_1 = __importDefault(require("./Base")); const parseContent_1 = __importDefault(require("../../functions/parseContent")); class WsOpenContext extends Base_1.default { constructor(context, rawContext, abort, type = 'open') { super(context); this.rawContext = rawContext; this.abort = abort; this.type = type; } /** * Websocket Close (Abort) Controller (please use to decrease server load) * @since 9.0.0 */ $abort(callback) { if (callback) this.abort.addEventListener('abort', callback); return this.abort.aborted; } /** * Close the Socket and send a Code + Message to the Client (automatically Formatted) * * This will instantly close the socket connection with a status code and * message of choice, after calling and successfully closing the `.onClose()` * callback will be called to finish the task. * @example * ``` * ctr.close(1011, 'An Error has occured') * ``` * @since 5.4.0 */ close(code, reason) { this.rawContext.close(code, reason); return this; } /** * Print a Message to the Client (automatically Formatted) * * This Message will instantly sent to the client, since this * is a websocket, this also means that the message cannot be * overriden after this function is called. * @example * ``` * await ctr.print({ * message: 'this is json!' * }) * * // content will be `{"message":"this is json!"}` * * /// or * * await ctr.print({ * message: 'this is json!' * }, true) * // content will be `{\n "message": "this is json!"\n}` * * /// or * * await ctr.print('this is text!') * // content will be `this is text!` * ``` * @since 5.4.0 */ async print(type, content, prettify = false) { const parsed = await (0, parseContent_1.default)(content, prettify); this.rawContext.write(type, parsed.content, this.global.options.compression.ws.enabled && this.global.options.compression.ws.maxSize > parsed.content.byteLength); return this; } /** * Print a channels value to the client * * This will print when the provided channel has a new value, * basically subscribing to the channel. * @example * ``` * const channel = new Channel<string>() * * ctr.printChannel(channel) * * ref.send('Ok') * ``` * @since 9.0.0 */ printChannel(channel) { if (!channel['onPublish']) { this.context.global.channels.push(channel); channel['onPublish'] = (type, data) => { this.context.global.implementation.wsPublish(type, channel.id, data, this.global.options.compression.ws.enabled && this.global.options.compression.ws.maxSize > data.byteLength); }; } this.rawContext.subscribe(channel.id); return this; } /** * Remove a channel from the client * * This will remove the subscription to the channel * from the client. No more messages will be sent. * @example * ``` * const channel = new Channel<string>() * * ctr.printChannel(channel) * * ref.send('Ok') * * ctr.removeChannel(channel) * * ref.send('No') // will not be sent * ``` * @since 9.0.0 */ removeChannel(channel) { this.rawContext.unsubscribe(channel.id); return this; } } exports.default = WsOpenContext;