forked from pull-stream/pull-ws
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds an optional `heartbeatMs` key to the server config that will ping each connected client on that interval - if they have not responded with a pong since the last interval, the client will be disconnected. Fixes #113
- Loading branch information
1 parent
9881c23
commit 138a3ee
Showing
4 changed files
with
99 additions
and
3 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,5 +1,5 @@ | ||
export { default as duplex } from './duplex.js' | ||
export { default as source } from './source.js' | ||
export { default as sink } from './sink.js' | ||
export { createServer } from './server.js' | ||
export { createServer, type WebSocketServer } from './server.js' | ||
export { connect } from './client.js' |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { expect } from 'aegir/chai' | ||
import delay from 'delay' | ||
import Sinon from 'sinon' | ||
import { isNode, isElectronMain } from 'wherearewe' | ||
import * as WS from '../src/index.js' | ||
import WebSocket from '../src/web-socket.js' | ||
|
||
describe('ping', () => { | ||
if (!(isNode || isElectronMain)) { | ||
return | ||
} | ||
|
||
let server: WS.WebSocketServer | ||
let client: WebSocket | ||
|
||
afterEach(async () => { | ||
if (client != null) { | ||
client.close() | ||
} | ||
|
||
if (server != null) { | ||
await server.close() | ||
} | ||
}) | ||
|
||
it('server should ping connected clients', async () => { | ||
server = WS.createServer({ | ||
heartbeatMs: 10 | ||
}) | ||
await server.listen(55214) | ||
|
||
client = new WebSocket('http://127.0.0.1:55214') | ||
const pongSpy = Sinon.spy(client, 'pong') | ||
|
||
await delay(200) | ||
|
||
expect(client).to.have.property('readyState', WebSocket.OPEN) | ||
expect(pongSpy).to.have.property('called', true) | ||
}) | ||
|
||
it('server should disconnected unresponsive clients', async () => { | ||
server = WS.createServer({ | ||
heartbeatMs: 10 | ||
}) | ||
await server.listen(55214) | ||
|
||
client = new WebSocket('http://127.0.0.1:55214') | ||
|
||
// make sure the client will not respond to a pong | ||
client.pong = () => {} | ||
|
||
await delay(200) | ||
|
||
expect(client).to.have.property('readyState', WebSocket.CLOSED) | ||
}) | ||
}) |