export type UseWebSocketSendable = string | ArrayBufferLike | Blob | ArrayBufferView; export interface UseWebSocketOptionsHeartbeat { /** * The heartbeat message to send to the server. * * @defaultValue 'ping' */ message?: UseWebSocketSendable; /** * The interval at which to send a heartbeat message to the server, disabled when set to `0`. * * @defaultValue 1_000 */ interval?: number; /** * The timeout for the server to respond to the heartbeat message, set false to disable. * * @defaultValue 1_000 */ responseTimeout?: number; /** * The expected response heartbeat message from the server, `true` means any message. * * @defaultValue true */ responseMessage?: true | UseWebSocketSendable | ((data: UseWebSocketSendable) => boolean); } export interface UseWebSocketOptionsReconnect { /** * The number of times to reconnect, default is `() => true`, reconnect indefinitely. */ count?: number | (() => boolean); /** * The interval at which to reconnect. */ interval?: number; /** * A callback function that is called when the WebSocket connection is closed and the reconnection limit is reached. */ onFailed?: (event: CloseEvent, ws: WebSocket) => void; } export interface UseWebSocketOptions { /** * A callback function that is called when the WebSocket connection is opened. * * @defaultValue undefined */ onOpen?: (event: Event, ws: WebSocket) => void; /** * A callback function that is called when the WebSocket connection is closed. * * @defaultValue undefined */ onClose?: (event: CloseEvent, ws: WebSocket) => void; /** * A callback function that is called when an error occurs. * * @defaultValue undefined */ onError?: (event: Event, ws: WebSocket) => void; /** * A callback function that is called when a message is received from the server. * * @defaultValue undefined */ onMessage?: (event: MessageEvent, ws: WebSocket) => void; /** * A callback function that is called when the WebSocket connection fails to open, * such as an error or an invalid URL, which will cause the ws to fail to create a connection and close the connection. * * @defaultValue undefined */ onOpenFailed?: (event: CloseEvent, ws: WebSocket) => void; /** * Whether to send a heartbeat message to the server. * * @defaultValue false */ heartbeat?: boolean | UseWebSocketOptionsHeartbeat; /** * Whether to reconnect when the connection is closed. Count can be a number or a function that returns a boolean. * * @defaultValue true => { count: () => true, interval: 1_000 } */ reconnect?: boolean | UseWebSocketOptionsReconnect; /** * Whether to connect to the server immediately. * * @defaultValue true when `wsUrl` is provided, otherwise false */ immediate?: boolean; /** * Whether to close the WebSocket connection when the component is unmounted. * * @defaultValue true */ closeOnUnmount?: boolean; /** * The WebSocket subprotocols to use. * * @defaultValue [] */ protocols?: string | string[]; /** * Filter the message to `onMessage` callback, return `true` to ignore the message. * * Useful for filtering out heartbeat messages. * * @defaultValue undefined */ filter?: (event: MessageEvent, ws: WebSocket) => boolean; } export interface UseWebSocketReturns { /** * The WebSocket instance. */ ws: WebSocket | null; /** * A function to send data to the server. */ send: (data: UseWebSocketSendable) => void; /** * The current ready state of the WebSocket connection. */ readyState: UseWebSocketReturnsReadyState; /** * Close the WebSocket connection. */ close: () => Promise; /** * Open the WebSocket connection. */ open: HasURL extends true ? () => Promise : (wsUrl?: string, protocols?: string | string[]) => Promise; } export type UseWebSocketReturnsReadyState = typeof WebSocket.CONNECTING | typeof WebSocket.OPEN | typeof WebSocket.CLOSING | typeof WebSocket.CLOSED; /** * A simplified React Hook for using WebSockets, wrapping the native WebSocket API. It supports features such as automatic reconnection and heartbeat. * * @since 1.7.0 */ export declare function useWebSocket(wsUrl?: string, options?: UseWebSocketOptions): UseWebSocketReturns; export declare function useWebSocket(options?: Omit): UseWebSocketReturns;