import * as ws from 'ws'; import { SessionRequest, OrisonServer, SessionState } from './server'; export declare type WebSocketHandler = (context: { req: SessionRequest; data: any; socket: ws; service: WebSocketService; }) => Promise; export default class WebSocketService { private subscriptions; private endpoints; initialize(): void; private getEndpoints; handleConnection(_: OrisonServer, socket: ws, req: SessionRequest): void; addSubscription(subscription: Subscription): void; /** * Gets a Subscription added with `addSubscription` by its path name. * @param path the path of the subscription */ getSubscription(path: string): Subscription; } export declare class Subscription { path: string; options?: SubscriptionOptions | undefined; listeners: [ws, SessionRequest][]; constructor(path: string, options?: SubscriptionOptions | undefined); /** * Calls the given function for each subscribed clients, and sends them the data returned by the function. * If the data returned by the function is `null` or `undefined`, then the client will not be sent any data. * @param dataGenerator the generator for the data to send */ broadcast(dataGenerator: (state: SessionState) => any): void; } export interface SubscriptionArgs { req: SessionRequest; socket: ws; data?: any; } export interface SubscriptionOptions { /** * Called when a client socket requests to subscribe. * @returns data to return to the client, or `null` or `undefined` if they should not be added to the list of subscribed sockets. * An empty object (`{}`) will add the client to the list. */ onSubscribeRequest?: (args: SubscriptionArgs) => Promise; /** * Called when a client requests to unsubscribe. * @returns data to return to the client, or `null` or `undefined` if no data */ onUnsubscribeRequest?: (args: SubscriptionArgs) => Promise; }