@minimaltech/node-infra
Version:
Minimal Technology NodeJS Infrastructure - Loopback 4 Framework
66 lines (65 loc) • 2.58 kB
TypeScript
import { BaseHelper } from '../../base/base.helper';
import { ValueOrPromise } from '../../common';
import { TcpSocketConnectOpts as PlainConnectionOptions, Socket as PlainSocketClient } from 'node:net';
import { ConnectionOptions as TlsConnectionOptions, TLSSocket as TlsSocketClient } from 'node:tls';
export interface INetworkTcpClientProps<SocketClientOptions extends PlainConnectionOptions | TlsConnectionOptions, SocketClientType extends PlainSocketClient | TlsSocketClient> {
identifier: string;
scope?: string;
options: SocketClientOptions;
reconnect?: boolean;
maxRetry?: number;
encoding?: BufferEncoding;
createClientFn: (options: SocketClientOptions, connectionListener?: () => void) => SocketClientType;
onConnected?: (opts: {
client: SocketClientType;
}) => ValueOrPromise<void>;
onData?: (opts: {
identifier: string;
message: string | Buffer;
}) => ValueOrPromise<void>;
onClosed?: (opts: {
client: SocketClientType;
}) => void;
onError?: (error: any) => void;
}
export declare class BaseNetworkTcpClient<SocketClientOptions extends PlainConnectionOptions | TlsConnectionOptions, SocketClientType extends PlainSocketClient | TlsSocketClient> extends BaseHelper {
protected client?: SocketClientType | null;
protected options: SocketClientOptions;
protected reconnect?: boolean;
protected retry: {
maxReconnect: number;
currentReconnect: number;
};
protected reconnectTimeout: any;
protected encoding?: BufferEncoding;
protected createClientFn: (options: SocketClientOptions, connectionListener?: () => void) => SocketClientType;
protected onConnected: (opts: {
client: SocketClientType;
}) => void;
protected onData: (opts: {
identifier: string;
message: string | Buffer;
}) => void;
protected onClosed?: (opts: {
client: SocketClientType;
}) => void;
protected onError?: (error: any) => void;
constructor(opts: INetworkTcpClientProps<SocketClientOptions, SocketClientType>);
getClient(): SocketClientType | null | undefined;
handleConnected(): void;
handleData(_opts: {
identifier: string;
message: string | Buffer;
}): void;
handleClosed(): void;
handleError(error: any): void;
connect(opts: {
resetReconnectCounter: boolean;
}): void;
disconnect(): void;
forceReconnect(): void;
isConnected(): boolean | null | undefined;
emit(opts: {
payload: Buffer | string;
}): void;
}