Skip to content

Commit

Permalink
feat(useWebSocket): add immediate option (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed May 12, 2021
1 parent d6178cb commit 38460f0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/core/useWebSocket/index.md
Expand Up @@ -11,11 +11,17 @@ Reactive [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/
```js
import { useWebSocket } from '@vueuse/core'

const { status, data, send, close } = useWebSocket('ws://websocketurl')
const { status, data, send, open, close } = useWebSocket('ws://websocketurl')
```

See the [Type Declarations](#type-declarations) for more options.

### Immediate

Auto connect (disabled by default, will be enabled by default in the future).

This will call `open()` automatically for you and you don't need to call it by yourself.

### Auto-reconnection

Reconnect on errors automatically (disabled by default).
Expand Down Expand Up @@ -119,6 +125,13 @@ export interface WebSocketOptions {
*/
onFailed?: Fn
}

/**
* Automatically open a connection
*
* @default false
*/
immediate?: boolean
}
export interface WebSocketResult<T> {
/**
Expand Down
9 changes: 9 additions & 0 deletions packages/core/useWebSocket/index.ts
Expand Up @@ -55,6 +55,13 @@ export interface WebSocketOptions {
*/
onFailed?: Fn
}

/**
* Automatically open a connection
*
* @default false
*/
immediate?: boolean
}

export interface WebSocketResult<T> {
Expand Down Expand Up @@ -212,6 +219,8 @@ export function useWebSocket<Data = any>(
heartbeatResume = resume
}

if (options.immediate) _init()

const open = () => {
close()
retried = 0
Expand Down

0 comments on commit 38460f0

Please sign in to comment.