-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: bug where heartbeat wasn't working (#18)
* fix: bug where heartbeat wasn't working * chore: improve types * style: format code * test: add new test for heartbeat, update another tests * chore: remove unused code from vite.config
- Loading branch information
Showing
18 changed files
with
205 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,32 @@ | ||
import { send } from '../send' | ||
import type { WSGOConfig } from '../types' | ||
import type { WSGOConfig, WSGOMessage } from '../types' | ||
|
||
const heartbeatMessage = 'ping' | ||
const heartbeatInterval = 1000 | ||
const heartbeatTimeout = 1000 | ||
|
||
let heartbeatTimeout: ReturnType<typeof setTimeout> | undefined | ||
let heartbeatTimeoutWait: ReturnType<typeof setTimeout> | undefined | ||
|
||
export function heartbeatStart(ws: WebSocket, _config: WSGOConfig): void { | ||
heartbeatStop() | ||
|
||
heartbeatTimeout = setTimeout(() => { | ||
setTimeout(() => { | ||
send(ws, _config, heartbeatMessage, heartbeatMessage) | ||
heartbeatTimeoutWait = setTimeout(() => { | ||
// open() | ||
ws.close() | ||
}, heartbeatInterval + heartbeatTimeout) | ||
}, heartbeatInterval) | ||
} | ||
|
||
export function heartbeatStop(): void { | ||
clearTimeout(heartbeatTimeout) | ||
heartbeatTimeout = undefined | ||
} | ||
|
||
export function listenHeartbeat(ws: WebSocket, _config: WSGOConfig, e: MessageEvent<any>): void { | ||
if (e.data === heartbeatMessage) { | ||
export function heartbeatListen(ws: WebSocket, _config: WSGOConfig, e: WSGOMessage<any>): void { | ||
if (e.event === 'pong') { | ||
heartbeatStop() | ||
heartbeatStart(ws, _config) | ||
// eslint-disable-next-line no-useless-return | ||
return | ||
} | ||
} | ||
|
||
export function heartbeatStop(): void { | ||
clearTimeout(heartbeatTimeoutWait) | ||
heartbeatTimeoutWait = undefined | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { WSGOMessage } from '../types' | ||
|
||
export interface WSGOSubscribeResponse<T = any> extends WSGOMessage<T> { | ||
/** Time when the client received the event */ | ||
timeReceived: number | ||
} | ||
|
||
export type WSGOSubscribeCallback<T> = (message: WSGOSubscribeResponse<T>) => any |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.