import { Accessor } from 'solid-js'; import { Fn, MaybeAccessor } from '@solidjs-use/shared'; type WebSocketStatus = 'OPEN' | 'CONNECTING' | 'CLOSED'; interface UseWebSocketOptions { onConnected?: (ws: WebSocket) => void; onDisconnected?: (ws: WebSocket, event: CloseEvent) => void; onError?: (ws: WebSocket, event: Event) => void; onMessage?: (ws: WebSocket, event: MessageEvent) => void; /** * Send heartbeat for every x milliseconds passed * * @default false */ heartbeat?: boolean | { /** * Message for the heartbeat * * @default 'ping' */ message?: string | ArrayBuffer | Blob; /** * Interval, in milliseconds * * @default 1000 */ interval?: number; /** * Heartbeat response timeout, in milliseconds * * @default 1000 */ pongTimeout?: number; }; /** * Enabled auto reconnect * * @default false */ autoReconnect?: boolean | { /** * Maximum retry times. * * Or you can pass a predicate function (which returns true if you want to retry). * * @default -1 */ retries?: number | (() => boolean); /** * Delay for reconnect, in milliseconds * * @default 1000 */ delay?: number; /** * On maximum retry times reached. */ onFailed?: Fn; }; /** * Automatically open a connection * * @default true */ immediate?: boolean; /** * Automatically close a connection * * @default true */ autoClose?: boolean; /** * List of one or more sub-protocol strings * * @default [] */ protocols?: string[]; } interface UseWebSocketReturn { /** * Reference to the latest data received via the websocket, * can be watched to respond to incoming messages */ data: Accessor; /** * The current websocket status, can be only one of: * 'OPEN', 'CONNECTING', 'CLOSED' */ status: Accessor; /** * Closes the websocket connection gracefully. */ close: WebSocket['close']; /** * Reopen the websocket connection. * If there the current one is active, will close it before opening a new one. */ open: Fn; /** * Sends data through the websocket connection. * * @param data * @param useBuffer when the socket is not yet open, store the data into the buffer and sent them one connected. Default to true. */ send: (data: string | ArrayBuffer | Blob, useBuffer?: boolean) => boolean; /** * Reference to the WebSocket instance. */ ws: Accessor; } /** * Reactive WebSocket client. * * @see https://solidjs-use.github.io/solidjs-use/core/useWebSocket */ declare function useWebSocket(url: MaybeAccessor, options?: UseWebSocketOptions): UseWebSocketReturn; export { UseWebSocketOptions, UseWebSocketReturn, WebSocketStatus, useWebSocket };