diff --git a/packages/core/useWebSocket/index.md b/packages/core/useWebSocket/index.md index de4ca9b10e39..f09aed6d6b79 100644 --- a/packages/core/useWebSocket/index.md +++ b/packages/core/useWebSocket/index.md @@ -69,6 +69,18 @@ const { status, data, close } = useWebSocket('ws://websocketurl', { }) ``` +### Sub-protocols + +List of one or more subprotocols to use, in this case soap and wamp. + +```js +import { useWebSocket } from '@vueuse/core' + +const { status, data, send, open, close } = useWebSocket('ws://websocketurl', { + protocols: ['soap'], // ['soap', 'wamp'] +}) +``` + ## Type Declarations @@ -131,6 +143,12 @@ export interface WebSocketOptions { * @default true */ immediate?: boolean + /** + * List of one or more sub-protocol strings + * + * @default [] + */ + protocols?: string[], } export interface WebSocketResult { /** @@ -172,7 +190,7 @@ export interface WebSocketResult { */ export declare function useWebSocket( url: string, - options?: WebSocketOptions + options?: WebSocketOptions, ): WebSocketResult ``` @@ -180,5 +198,4 @@ export declare function useWebSocket( [Source](https://github.com/vueuse/vueuse/blob/main/packages/core/useWebSocket/index.ts) • [Docs](https://github.com/vueuse/vueuse/blob/main/packages/core/useWebSocket/index.md) - diff --git a/packages/core/useWebSocket/index.ts b/packages/core/useWebSocket/index.ts index d24f08d8b32b..20ebb9558d0e 100644 --- a/packages/core/useWebSocket/index.ts +++ b/packages/core/useWebSocket/index.ts @@ -62,6 +62,13 @@ export interface WebSocketOptions { * @default true */ immediate?: boolean + + /** + * List of one or more sub-protocol strings + * + * @default [] + */ + protocols?: string[] } export interface WebSocketResult { @@ -124,6 +131,7 @@ export function useWebSocket( onError, onMessage, immediate = true, + protocols = [], } = options const data: Ref = ref(null) @@ -166,7 +174,7 @@ export function useWebSocket( } const _init = () => { - const ws = new WebSocket(url) + const ws = new WebSocket(url, protocols) wsRef.value = ws status.value = 'CONNECTING' explicitlyClosed = false