From 75330572490e77bad7d8cabebfb6708d1344f7a4 Mon Sep 17 00:00:00 2001 From: "Brian E. Granger" Date: Sat, 3 Oct 2020 14:30:22 -0700 Subject: [PATCH] Add clearInterval calls to clean up closure. --- bin/utils.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/bin/utils.js b/bin/utils.js index 27e1b9b..08ba451 100644 --- a/bin/utils.js +++ b/bin/utils.js @@ -216,9 +216,7 @@ exports.setupWSConnection = (conn, req, { docName = req.url.slice(1).split('?')[ doc.conns.set(conn, new Set()) // listen and reply to events conn.on('message', /** @param {ArrayBuffer} message */ message => messageListener(conn, doc, new Uint8Array(message))) - conn.on('close', () => { - closeConn(doc, conn) - }) + // Check if connection is still alive let pongReceived = true const pingInterval = setInterval(() => { @@ -233,12 +231,18 @@ exports.setupWSConnection = (conn, req, { docName = req.url.slice(1).split('?')[ conn.ping() } catch (e) { closeConn(doc, conn) + clearInterval(pingInterval) } } }, pingTimeout) + conn.on('close', () => { + closeConn(doc, conn) + clearInterval(pingInterval) + }) conn.on('pong', () => { pongReceived = true }) + // send sync step 1 const encoder = encoding.createEncoder() encoding.writeVarUint(encoder, messageSync)