From 49b75cec60fec9c8a42119b124a9c54d29d30308 Mon Sep 17 00:00:00 2001 From: Denis Badurina Date: Wed, 26 Aug 2020 18:16:43 +0200 Subject: [PATCH] fix(client): send complete message and close only if socket is still open --- src/client.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/client.ts b/src/client.ts index 2c5c3dfa..ba2dfad4 100644 --- a/src/client.ts +++ b/src/client.ts @@ -232,19 +232,23 @@ export function createClient(options: ClientOptions): Client { return () => { if (socket) { - socket.send( - stringifyMessage({ - id: uuid, - type: MessageType.Complete, - }), - ); + if (socket.readyState === WebSocket.OPEN) { + socket.send( + stringifyMessage({ + id: uuid, + type: MessageType.Complete, + }), + ); + } socket.removeEventListener('message', handleMessage); // equal to 1 because this sink is the last one. // the deletion from the map happens afterwards if (Object.entries(subscribedSinks).length === 1) { - socket.close(1000, 'Normal Closure'); + if (socket.readyState === WebSocket.OPEN) { + socket.close(1000, 'Normal Closure'); + } socket = null; } }