Browse Source

fixed receiving one JSON object over multiple packets

master
mortie 6 years ago
parent
commit
7fe39817c9
1 changed files with 11 additions and 9 deletions
  1. 11
    9
      js/rpcconn.js

+ 11
- 9
js/rpcconn.js View File

obj = JSON.parse(json); obj = JSON.parse(json);
} catch (err) { } catch (err) {
console.log("Failed to parse JSON: '"+json+"'"); console.log("Failed to parse JSON: '"+json+"'");
throw err;
return;
} }


if (obj.id == null) { if (obj.id == null) {
//Digest JSON lines as they come, but only if they're a whole line //Digest JSON lines as they come, but only if they're a whole line
var currdata = ""; var currdata = "";
this.sock.on("data", d => { this.sock.on("data", d => {
var s = d.toString();
currdata += s;
var arr = s.split("\n");
while (arr[1] != null) {
onjson(arr.shift());
arr.shift();
currdata += d.toString();

var start = 0;
for (var i = 0; i < currdata.length; ++i) {
if (currdata[i] === "\n") {
var s = currdata.substring(start, i);
start = i + 1;
onjson(s);
}
} }
currdata = arr[0] || "";
currdata = currdata.substring(start);
}); });
} }


else resolve(res); else resolve(res);
} }


console.log("Sending '"+json+"'");
this.sock.write(json+"\n"); this.sock.write(json+"\n");
this._id += 1; this._id += 1;
}); });

Loading…
Cancel
Save