rjweb-server
Version:
Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS
130 lines (129 loc) • 3.99 kB
TypeScript
/// <reference types="node" />
import Server from "../server";
import { LocalContext } from "../../types/context";
import Base from "./Base";
import { WebSocket } from "@rjweb/uws";
import { WebSocketContext } from "../../types/webSocket";
import { Content } from "../../functions/parseContent";
import { Readable } from "stream";
import Reference from "../reference";
export default class WSConnect<Context extends Record<any, any> = {}, Type = 'connect', Path extends string = '/'> extends Base<Context, Path> {
/**
* Initializes a new Instance of a Web Context
* @since 7.0.0
*/ constructor(controller: Server<any, any>, localContext: LocalContext, ws: WebSocket<WebSocketContext>);
/**
* The Type of this Request
* @since 5.7.0
*/ readonly type: Type;
/**
* The Raw HTTP Server Ws Variable
* @since 7.0.0
*/ readonly rawWs: WebSocket<WebSocketContext>;
/**
* 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(401, {
* message: 'this is json!'
* })
*
* // or
*
* ctr.close(401, 'this is text!')
* ```
* @since 5.4.0
*/ close(code: number, message?: Content): this;
/**
* Print a Message to the Client (automatically Formatted)
*
* This will send a new websocket message to the client as soon
* as the event loop allows it to execute the async task of parsing
* the message content.
* @example
* ```
* ctr.print({
* message: 'this is json!'
* })
*
* // or
*
* ctr.print('this is text!')
* ```
* @since 5.4.0
*/ print(message: Content, options?: {
/**
* Whether to prettify output (mostly just JSONs)
* @default false
* @since 6.2.0
*/ prettify?: boolean;
}): this;
/**
* Print a references value every time it changes
*
* This will print when the provided reference changes state similarly
* to the `.printStream()` method which listen to a streams `data` event.
* @example
* ```
* const ref = new Reference('Hello')
*
* ctr.printRef(ref)
*
* ref.set('Ok')
* ```
* @since 7.2.0
*/ printRef(reference: Reference, options?: {
/**
* Whether to prettify output (currently just JSONs)
* @default false
* @since 7.4.0
*/ prettify?: boolean;
}): this;
/**
* Remove a reference subscription
*
* This will remove the listener of a reference from the
* current socket. May be slow when having many references
* attached to the socket.
* @example
* ```
* const ref = new Reference('Hello')
*
* ctr.printRef(ref)
*
* ref.set('Ok')
*
* ctr.removeRef(ref)
* ```
* @since 7.2.0
*/ removeRef(reference: Reference): this;
/**
* Print the `data` event of a Stream to the Client
*
* This will print the `data` event of a stream to the client
* in real time. This shouldnt be used over `.printRef()` but is
* useful when working with something like a `fs.ReadStream` for
* some reason.
* @example
* ```
* const fileStream = fs.createReadStream('./profile.png')
* ctr.printStream(fileStream)
* ```
* @since 5.4.0
*/ printStream(stream: Readable, options?: {
/**
* Whether to prettify output (currently just JSONs)
* @default false
* @since 7.4.0
*/ prettify?: boolean;
/**
* Whether to Destroy the Stream if the Socket gets closed
* @default true
* @since 5.4.0
*/ destroyAbort?: boolean;
}): this;
}