Skip to content

Commit

Permalink
fix: support fragmented and multiple packets from remote scan process (
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone authored Dec 11, 2023
1 parent 4a19320 commit 0ccb454
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions admin/build/index.js.map

Large diffs are not rendered by default.

18 changes: 13 additions & 5 deletions build/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions build/main.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/scanProcess.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ function connectToBLEServer() {
socket = new Socket();

const reviver = getMessageReviver(handleMessage);
let receiveBuffer = "";

socket
.on("close", () => {
Expand All @@ -280,11 +281,20 @@ function connectToBLEServer() {
adapter.setState("info.connection", true, true);
})
.on("data", (data) => {
try {
const msg = JSON.parse(data.toString());
reviver(msg);
} catch (e) {
console.error(e);
// Each message ends with a newline. We might receive multiple messages at once,
// and we might also receive incomplete messages.
receiveBuffer += data.toString();
let newlineIndex: number;
while ((newlineIndex = receiveBuffer.indexOf("\n")) > -1) {
const line = receiveBuffer.slice(0, newlineIndex);
receiveBuffer = receiveBuffer.slice(newlineIndex + 1);
try {
const msg = JSON.parse(line);
reviver(msg);
} catch (e) {
console.error(e);
console.error("data was: " + line);
}
}
});

Expand Down

0 comments on commit 0ccb454

Please sign in to comment.