-
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.
* feat: add heartbeat mechanism * feat: add heartbeat to core * test: fix ts * test: remove timeReceived * test: add timeout for await message
- Loading branch information
Showing
7 changed files
with
163 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { send } from './send' | ||
|
||
const heartbeatMessage = 'ping' | ||
const heartbeatInterval = 1000 | ||
|
||
let heartbeatTimeout: ReturnType<typeof setTimeout> | undefined | ||
|
||
export function heartbeatStart(ws: WebSocket): void { | ||
heartbeatStop() | ||
|
||
heartbeatTimeout = setTimeout(() => { | ||
send(heartbeatMessage, heartbeatMessage, ws) | ||
}, heartbeatInterval) | ||
} | ||
|
||
export function heartbeatStop(): void { | ||
clearTimeout(heartbeatTimeout) | ||
heartbeatTimeout = undefined | ||
} | ||
|
||
export function listenHeartbeat(ws: WebSocket, e: MessageEvent<any>): void { | ||
if (e.data === heartbeatMessage) { | ||
heartbeatStart(ws) | ||
// eslint-disable-next-line no-useless-return | ||
return | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { describe, it, expect, vi, beforeAll, afterAll } from 'vitest' | ||
import WSGO from '../src/index' | ||
import ws from 'ws' | ||
import { createMockWSServer } from './utils' | ||
|
||
describe('open', () => { | ||
const date = new Date(2000, 1, 1) | ||
|
||
let port: number = 0 | ||
let server: ws.Server | ||
|
||
beforeAll(() => { | ||
const mockWSServer = createMockWSServer(port) | ||
|
||
server = mockWSServer.server | ||
port = mockWSServer.port | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
it('should send a ping event and receive a pong response', async () => { | ||
const eventName = 'pong' | ||
|
||
let event: any | ||
|
||
// Arrange | ||
const wsgo = WSGO(`ws://localhost:${port}`) | ||
await vi.waitFor(() => { | ||
vi.setSystemTime(date) | ||
if (wsgo.ws?.readyState !== window.WebSocket.OPEN) { | ||
throw new Error() | ||
} | ||
}) | ||
|
||
// Act | ||
wsgo.subscribe(eventName, (e) => (event = e)) | ||
await vi.waitFor( | ||
() => { | ||
vi.setSystemTime(date) | ||
if (event === undefined) { | ||
throw new Error('Message not received back') | ||
} | ||
}, | ||
{ | ||
timeout: 5000, | ||
interval: 250, | ||
}, | ||
) | ||
|
||
// Assert | ||
expect(event).toStrictEqual({ event: eventName, timeSended: Date.now() }) | ||
}) | ||
|
||
it.todo('must close the connection if no response is received from the server') | ||
}) |
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,28 @@ | ||
import ws from 'ws' | ||
|
||
export function createMockWSServer(port: number = 0): { server: ws.WebSocketServer; port: number } { | ||
let server = new ws.WebSocketServer({ port }) | ||
|
||
server.on('connection', (ws) => { | ||
ws.on('message', (data, isBinary) => { | ||
const parsedData = isBinary ? data : (JSON.parse(data.toString()) as any) | ||
|
||
if (parsedData.event === 'ping') { | ||
const message = { event: 'pong', timeSended: Date.now() } | ||
ws.send(JSON.stringify(message)) | ||
} | ||
|
||
const message = { ...data, timeSended: Date.now() } | ||
|
||
// setTimeout(() => { | ||
// ws.send(JSON.stringify(message)) | ||
// }, 1000) | ||
ws.send(JSON.stringify(message)) | ||
}) | ||
}) | ||
|
||
return { | ||
server, | ||
port: (server.address() as ws.AddressInfo).port, | ||
} | ||
} |