From 591420614766c747f06b39e5c5600a1d99f27bce Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Fri, 9 Nov 2018 07:36:00 +0100 Subject: [PATCH] [doc] Fix nits --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1825676d4..cae947caa 100644 --- a/README.md +++ b/README.md @@ -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); @@ -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); @@ -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);