import http from 'node:http'; import WebSocket from 'ws'; import { Request } from './types.js'; import Stream from 'node:stream'; import { DefaultMiddlewareOptions } from '@gaman/common/types/middleware.types.js'; export type Websocket = { factory: WebsocketHandler; }; export type WebsocketContext = { clientId: string; request: Request; socket: Stream.Duplex; clients: Array; close(code?: number, message?: any): any; broadcast(message: any, withoutMe?: boolean): any; send(message: any): any; } & Omit; export type WebsocketHandler = (handler: WebsocketContext & WebsocketEvent) => any; export interface WebsocketEvent { onDisconnect: (callback: (code?: number, reason?: string) => any | Promise) => any; onMessage: (callback: (message: any) => any | Promise) => any; onError: (callback: (error: Error) => any | Promise) => any; onPing: (callback: (message: any) => any | Promise) => any; onPong: (callback: (message: any) => any | Promise) => any; } export interface WebsocketAdapter { listen: (server: http.Server, route: string, factory: WebsocketHandler) => void; } export type WebsocketMiddleware = { handler: WebsocketMiddlewareHandler; config?: Pick; }; export type WebsocketMiddlewareHandler = (ctx: WebsocketContext & { message: any; }, next: () => any | Promise) => any | Promise;