UNPKG

@supunlakmal/hooks

Version:

A collection of reusable React hooks

32 lines (31 loc) 1.2 kB
export declare enum ReadyState { Connecting = 0, Open = 1, Closing = 2, Closed = 3 } export interface UseWebSocketOptions { onOpen?: (event: WebSocketEventMap['open']) => void; onClose?: (event: WebSocketEventMap['close']) => void; onMessage?: (event: WebSocketEventMap['message']) => void; onError?: (event: WebSocketEventMap['error']) => void; reconnectLimit?: number; reconnectIntervalMs?: number; } export interface UseWebSocketReturn { sendMessage: (data: string | ArrayBuffer | Blob | ArrayBufferView) => void; lastMessage: MessageEvent | null; readyState: ReadyState; error: Event | null; connect: () => void; disconnect: () => void; getWebSocket: () => WebSocket | null; } /** * Custom hook for managing WebSocket connections. * * @param {string | URL | null} url The URL to connect to. If null, the connection is not initiated automatically. * @param {UseWebSocketOptions} [options={}] Hook options. * @returns {UseWebSocketReturn} Object containing the WebSocket state and control functions. */ export declare const useWebSocket: (url: string | URL | null, options?: UseWebSocketOptions) => UseWebSocketReturn;