@phnq/message
Version:
Asynchronous, incremental messaging client and server
26 lines (25 loc) • 1.13 kB
TypeScript
import type http from "node:http";
import type https from "node:https";
import MessageConnection from "./MessageConnection";
export type ConnectionId = string;
type ConnectHandler<T, R, A = never> = (conn: MessageConnection<T, R, A>, upgradeRequest: http.IncomingMessage) => Promise<void>;
type DisconnectHandler<T, R, A = never> = (conn: MessageConnection<T, R, A>) => Promise<void>;
type ReceiveHandler<T, R, A = never> = (conn: MessageConnection<T, R, A>, message: T) => Promise<R | AsyncIterableIterator<R>>;
interface Config {
httpServer: http.Server | https.Server;
path?: string;
paths?: string[];
}
declare class WebSocketMessageServer<T = unknown, R = T, A = never> {
private wss;
onConnect: ConnectHandler<T, R, A>;
onDisconnect: DisconnectHandler<T, R, A>;
onReceive: ReceiveHandler<T, R, A>;
private connectionsById;
constructor({ httpServer, path, paths }: Config);
getConnection(id: ConnectionId): MessageConnection<T, R, A> | undefined;
get connections(): MessageConnection<T, R, A>[];
close(): Promise<void>;
private start;
}
export default WebSocketMessageServer;