From 65342880544c21fe55d75f100703d46a5e65d0e6 Mon Sep 17 00:00:00 2001 From: Chinmay Pendharkar Date: Thu, 1 Aug 2024 16:50:22 +0800 Subject: [PATCH] fix(fjagejs): try to figure out if a send was successful and return the appopriate status --- gateways/js/src/TCPConnector.js | 2 +- gateways/js/src/WSConnector.js | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/gateways/js/src/TCPConnector.js b/gateways/js/src/TCPConnector.js index 2669fe9c..0daeb533 100644 --- a/gateways/js/src/TCPConnector.js +++ b/gateways/js/src/TCPConnector.js @@ -64,7 +64,7 @@ export default class TCPconnector { this.sock.on('connect', this._onSockOpen.bind(this)); this.sock.on('error', this._sockReconnect.bind(this)); this.sock.on('close', () => {this._sendConnEvent(false);}); - this.sock.send = data => {this.sock.write(data);}; + this.sock.send = data => this.sock.write(data); } catch (error) { if(this.debug) console.log('Connection failed to ', this.sock.host + ':' + this.sock.port); return; diff --git a/gateways/js/src/WSConnector.js b/gateways/js/src/WSConnector.js index 73979f1d..8e66625d 100644 --- a/gateways/js/src/WSConnector.js +++ b/gateways/js/src/WSConnector.js @@ -66,8 +66,22 @@ export default class WSConnector { this.sock.onmessage = event => { if (this._onWebsockRx) this._onWebsockRx.call(this,event.data); }; this._firstConn = false; this._firstReConn = true; - this.pendingOnOpen.forEach(cb => cb()); - this.pendingOnOpen.length = 0; + this.pendingOnOpen = this.pendingOnOpen.filter(cb => !cb()); + } + + /** + * Converts the websocket send API to return a boolean + * + * @param {String} msg message to be sent to the server + * @returns {boolean} true if the message was sent successfully + */ + _send(msg){ + try { + this.sock.send(msg+'\n'); + return true; + } catch (error) { + return false; + } } toString(){ @@ -79,16 +93,14 @@ export default class WSConnector { /** * Write a string to the connector * @param {string} s - string to be written out of the connector to the master + * @returns {boolean} - true if the string was written successfully */ write(s){ if (!this.sock || this.sock.readyState == this.sock.CONNECTING){ - this.pendingOnOpen.push(() => { - this.sock.send(s+'\n'); - }); + this.pendingOnOpen.push(() => this._send(s)); return true; } else if (this.sock.readyState == this.sock.OPEN) { - this.sock.send(s+'\n'); - return true; + return this.sock.send(s+'\n'); } return false; }