From 8b119b798215bc13d809d4d7026c0a3546911a7f Mon Sep 17 00:00:00 2001 From: Die4Ever Date: Sat, 30 Sep 2023 20:56:02 -0500 Subject: [PATCH] api chained receive test --- src/api.ts | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/api.ts b/src/api.ts index 34eb422..ca4f29a 100644 --- a/src/api.ts +++ b/src/api.ts @@ -11,6 +11,7 @@ class APIConnection onExpectedCloseCallback: (hadError: any) => void; onErrorCallback: (hadError: any) => void; onDataCallback: (hadError: any) => void; + buffer:string = ""; constructor(name:string, port:number, callback:CallableFunction) { this.name = name; @@ -109,23 +110,33 @@ class APIConnection let data: Object = null; let resp: Object = null; + // chop off the null-terminator + while(message[message.length-1] == '\0') + message = message.substring(0, message.length-1); + + // try try { - // chop off the null-terminator - while(message[message.length-1] == '\0') - message = message.substring(0, message.length-1); data = JSON.parse(message); - info(this.name+" received data: ", data); + this.buffer = ""; } catch(e) { - printException('error parsing '+this.name+' request JSON: ' + message, e); + try { + this.buffer += message; + data = JSON.parse(this.buffer); + this.buffer = ""; + } + catch(e) { + printException('error parsing '+this.name+' request JSON: ' + message, e); + return; + } } try { + info(this.name+" received data: ", data); resp = this.callback(data); + this.send(resp); } catch(e) { printException('error handling '+this.name+' request: ' + message, e); } - - //this.send(resp); } private reset_timeout() {