rjweb-server
Version:
Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS
59 lines (58 loc) • 1.79 kB
TypeScript
import { Content } from "../functions/parseContent";
import { RealAny } from "../types/internal";
type Listener<Value extends Content> = (data: Value) => RealAny;
export default class Channel<Value extends Content = Content> {
private data;
private listeners;
protected onPublish: ((type: 'text' | 'binary', data: ArrayBuffer) => void) | null;
/**
* Create a new Channel
* @example
* ```
* import { Server, Channel } from "rjweb-server"
* import { Runtime } from "@rjweb/runtime-bun"
*
* const echo = new Channel<string>()
*
* const server = new Server(Runtime, {
* port: 8000
* })
*
* server.path('/', (path) => path
* .ws('/echo', (ws) => ws
* .onOpen((ctr) => {
* ctr.printChannel(echo)
* })
* .onMessage((ctr) => {
* echo.send(ctr.rawMessage()) // will send the message to all subscribed sockets
* })
* )
* .http('GET', '/last-echo', (http) => http
* .onRequest((ctr) => {
* return ctr.print(echo.last())
* })
* )
* )
*
* server.start().then(() => console.log('Server Started!'))
* ```
* @since 9.0.0
*/ constructor(id?: number | null, initial?: Content);
/**
* The ID of the Channel used for publishing
* @since 9.0.0
*/ readonly id: number;
/**
* Send Data to each Subscriber (and listener)
* @since 9.0.0
*/ send(type: 'text' | 'binary', data: Value, prettify?: boolean): Promise<this>;
/**
* Get the last sent value
* @since 9.0.0
*/ last(): Value | null;
/**
* Listen for send events on this Channel
* @since 9.0.0
*/ listen(callback: Listener<Value>): () => void;
}
export {};