Skip to content

Commit

Permalink
fix(useWebsocket): reset pongTimeout on close (#3324)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
alipay404 and antfu committed Aug 25, 2023
1 parent 72cc6e4 commit aea27f8
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions packages/core/useWebSocket/index.ts
Expand Up @@ -171,15 +171,6 @@ export function useWebSocket<Data = any>(

let pongTimeoutWait: ReturnType<typeof setTimeout> | undefined

// Status code 1000 -> Normal Closure https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code
const close: WebSocket['close'] = (code = 1000, reason) => {
if (!wsRef.value)
return
explicitlyClosed = true
heartbeatPause?.()
wsRef.value.close(code, reason)
}

const _sendBuffer = () => {
if (bufferedData.length && wsRef.value && status.value === 'OPEN') {
for (const buffer of bufferedData)
Expand All @@ -193,6 +184,16 @@ export function useWebSocket<Data = any>(
pongTimeoutWait = undefined
}

// Status code 1000 -> Normal Closure https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code
const close: WebSocket['close'] = (code = 1000, reason) => {
if (!wsRef.value)
return
explicitlyClosed = true
resetHeartbeat()
heartbeatPause?.()
wsRef.value.close(code, reason)
}

const send = (data: string | ArrayBuffer | Blob, useBuffer = true) => {
if (!wsRef.value || status.value !== 'OPEN') {
if (useBuffer)
Expand Down

0 comments on commit aea27f8

Please sign in to comment.