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;