From a9cc9aa051f3c42139f37423c43ee7fb508ae764 Mon Sep 17 00:00:00 2001 From: dave-fl Date: Wed, 9 Feb 2022 17:05:34 -0500 Subject: [PATCH] Allow user to listen to close event (even if ws will reconnect) --- src/websocket.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/websocket.js b/src/websocket.js index 8a834b5b..c42831de 100644 --- a/src/websocket.js +++ b/src/websocket.js @@ -629,6 +629,10 @@ const userOpenHandler = (cb, transform = true) => () => { cb({ [transform ? 'eventType' : 'type']: 'open' }) } +const userCloseHandler = (cb, transform = true) => () => { + cb({ [transform ? 'eventType' : 'type']: 'close' }) +} + const userErrorHandler = (cb, transform = true) => error => { cb({ [transform ? 'eventType' : 'type']: 'error', error }) } @@ -713,6 +717,9 @@ const user = (opts, variator) => (cb, transform) => { if (opts.emitSocketOpens) { w.onopen = () => userOpenHandler(cb, transform)() } + if (opts.emitSocketCloses) { + w.onclose = () => userCloseHandler(cb, transform)() + } if (opts.emitSocketErrors) { w.onerror = ({ error }) => errorHandler(error) }