Skip to content

Commit

Permalink
[doc] Fix nits
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Nov 9, 2018
1 parent 3fa0e03 commit 5914206
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions README.md
Expand Up @@ -366,14 +366,14 @@ endpoint is still responsive.
```js
const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8080 });

function noop() {}

function heartbeat() {
this.isAlive = true;
}

const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', function connection(ws) {
ws.isAlive = true;
ws.on('pong', heartbeat);
Expand All @@ -397,6 +397,8 @@ without knowing it. You might want to add a ping listener on your clients to
prevent that. A simple implementation would be:

```js
const WebSocket = require('ws');

function heartbeat() {
clearTimeout(this.pingTimeout);

Expand All @@ -405,10 +407,10 @@ function heartbeat() {
// conservative assumption of the latency.
this.pingTimeout = setTimeout(() => {
this.terminate();
}, 30000 + 100);
}, 30000 + 1000);
}

const client = new WebSocket(url);
const client = new WebSocket('wss://echo.websocket.org/');

client.on('open', heartbeat);
client.on('ping', heartbeat);
Expand Down

0 comments on commit 5914206

Please sign in to comment.