fleeta-api-lib
Version:
A comprehensive library for fleet management applications - API, Auth, Device management
97 lines • 2.39 kB
TypeScript
/**
* GPS WebSocket Connection Manager
* Handles WebSocket connection lifecycle and reconnection logic
*/
import type { GpsConnectionState } from './types';
/**
* Connection configuration interface
*/
export interface ConnectionConfig {
url: string;
maxReconnectAttempts: number;
reconnectDelay: number;
heartbeatInterval?: number;
}
/**
* Connection event callbacks
*/
export interface ConnectionCallbacks {
onOpen?: () => void;
onClose?: (event: CloseEvent) => void;
onError?: (error: Event) => void;
onMessage?: (event: MessageEvent) => void;
onStateChange?: (state: GpsConnectionState) => void;
}
/**
* WebSocket dependency interface for testing
*/
export interface WebSocketDependency {
new (url: string): WebSocket;
}
/**
* Connection Manager class
* Separated connection logic for better testability and maintainability
*/
export declare class ConnectionManager {
private config;
private callbacks;
private WebSocketClass;
private ws;
private currentState;
private reconnectTimer;
private reconnectAttempts;
private isConnecting;
/**
* Create connection manager with dependency injection
*/
constructor(config: ConnectionConfig, WebSocketClass?: WebSocketDependency, callbacks?: ConnectionCallbacks);
/**
* Connect to WebSocket server
*/
connect(): void;
/**
* Disconnect from WebSocket server
*/
disconnect(): void;
/**
* Send message through WebSocket
*/
send(data: string): void;
/**
* Get current connection state
*/
getState(): GpsConnectionState;
/**
* Check if WebSocket is connected
*/
isConnected(): boolean;
/**
* Update connection configuration
*/
updateConfig(newConfig: Partial<ConnectionConfig>): void;
/**
* Update callbacks
*/
setCallbacks(callbacks: ConnectionCallbacks): void;
/**
* Destroy connection manager
*/
destroy(): void;
/**
* Setup WebSocket event handlers
*/
private setupWebSocketHandlers;
/**
* Schedule automatic reconnection
*/
private scheduleReconnect;
/**
* Clear reconnection timer
*/
private clearReconnectTimer;
/**
* Set connection state and notify callbacks
*/
private setState;
}
//# sourceMappingURL=ConnectionManager.d.ts.map