UNPKG

rjweb-server

Version:

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

94 lines (93 loc) 3.01 kB
/// <reference types="node" /> import InternalRequestContext from "../../types/internal/classes/RequestContext"; import { Content } from "../../types/global"; import Base from "./Base"; import { WsContext } from "../../types/implementation/contexts/ws"; import Channel from "../Channel"; export default class WsOpenContext<Type extends 'open' | 'message' = 'open', Context extends Record<any, any> = {}> extends Base<Context> { protected rawContext: WsContext; private abort; constructor(context: InternalRequestContext, rawContext: WsContext, abort: AbortSignal, type?: Type); /** * The Type of this Websocket Event * @since 5.7.0 */ readonly type: Type; /** * Websocket Close (Abort) Controller (please use to decrease server load) * @since 9.0.0 */ $abort(callback?: () => void): boolean; /** * 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?: number, reason?: string): 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 */ print(type: 'text' | 'binary', content: Content, prettify?: boolean): Promise<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: Channel<Content>): 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: Channel<Content>): this; }