///
///
///
import { IncomingMessage, Server as HTTPServer } from 'http';
import { Server as HTTPSServer } from 'https';
import WebSocket, { WebSocketServer } from 'ws';
import { Events, EventsEmit } from '@osmium/events';
import { WSMessage, WSSocket } from '../libs';
import { ObjectKeyString, SocketMetadata } from '../libs/types';
export declare class WSServer extends Events {
protected serializer: WSMessage.Serializer;
protected server: WebSocketServer;
clients: WSServer.Clients;
protected symbols: WSSocket.ISymbols;
isReady: boolean;
events: Events;
static createInstance(params: WSServer.Params): Promise;
constructor(params: WSServer.Params);
private registerHandlers;
private handleHandShake;
waitMessage(wsSocket: WebSocket, cb: (data: Buffer) => Promise | ReturnType, timeout?: number): Promise;
onConnect(cb: (...args: WSServer.Event.CONNECTED) => Promise | void): void;
onDisconnect(cb: (...args: WSServer.Event.DISCONNECTED) => Promise | void): void;
onWSEventListening(cb: () => void | Promise): void;
onWSEventHeaders(cb: (headers: string[], request: IncomingMessage) => void | Promise): void;
onWSEventConnection(cb: (socket: WebSocket, request: IncomingMessage) => void | Promise): void;
onWSEventError(cb: (error: Error) => void | Promise): void;
onWSEventClose(cb: () => void | Promise): void;
awaitReady(): Promise;
private prcessClients;
clientDisconnect(id: string): void;
clientsDisconnect(): void;
setMetadata(key: string, value: unknown): WSServer.Emits;
local(): WSServer.Emits;
to(destinations: string[] | string): WSServer.Emits;
timeout(value: number): WSServer.Emits;
firstResult(): WSServer.Emits;
}
export declare namespace WSServer {
namespace WS {
enum ServerEvents {
LISTENING = "listening",
HEADERS = "headers",
CONNECTION = "connection",
ERROR = "error",
CLOSE = "close"
}
const ClientIDPrefix: string;
}
enum event {
CONNECTED = "connected",
DISCONNECTED = "disconnected"
}
namespace Event {
type CONNECTED = [WSSocket.ID, WSSocket];
type DISCONNECTED = [WSSocket.ID | null];
}
type Metadata = {
wsServer: EmitMetadata;
[key: string]: any;
};
interface EmitMetadata {
to: string[] | null;
local: boolean;
timeout: number | null;
firstResult: boolean;
[key: string]: any;
}
type ClientsRow = {
client: WSSocket;
jwtToken: string;
};
type Clients = Map;
enum ServerMode {
Standalone = 0,
HTTP = 1
}
type ParamsHTTPServer = {
serverMode: ServerMode.HTTP;
server: HTTPServer | HTTPSServer;
path?: string;
};
type ParamsStandaloneServer = {
serverMode: ServerMode.Standalone;
port?: number;
host?: string;
};
type Params = ParamsHTTPServer | ParamsStandaloneServer;
class Emits extends EventsEmit {
metadata: EmitMetadata;
static createInstance(config?: Partial>, mapEmitOnce?: Events | null, metadata?: ObjectKeyString): Emits;
constructor(config?: Partial>, mapEmitOnce?: Events | null, metadata?: SocketMetadata);
setMetadata(key: string, value: unknown): Emits;
local(): Emits;
to(destinations: WSSocket.ID[] | WSSocket.ID): Emits;
timeout(value: number): Emits;
firstResult(): Emits;
}
}