From 2d409ba7a78d72cde85402bc21c478d8aad88ce9 Mon Sep 17 00:00:00 2001 From: adcpm Date: Wed, 22 May 2019 15:39:20 +0700 Subject: [PATCH] Add close method --- src/client.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/client.js b/src/client.js index 6313f03..ae9f674 100755 --- a/src/client.js +++ b/src/client.js @@ -21,6 +21,7 @@ export default class Client { constructor(address) { this.address = address; this.open = false; + this.shouldClose = false; this.queue = {}; this.notifications = () => {}; @@ -38,7 +39,12 @@ export default class Client { }); this.ws.addEventListener('open', () => { - this.open = true; + if (this.shouldClose) { + this.ws.close(); + this.shouldClose = false; + } else { + this.open = true; + } }); this.ws.addEventListener('close', () => { @@ -56,6 +62,14 @@ export default class Client { }); } + close() { + if (this.ws.readyState === WebSocket.CONNECTING) { + this.shouldClose = true; + } else { + this.ws.close(); + } + } + request(command, params, cb) { const request = { command }; if (params) request.params = params;