From b1ffc86636a6dd4e3d8f5d377b8f379e5e478c2d Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <67171972+IoTManagerProject@users.noreply.github.com> Date: Mon, 24 Jan 2022 00:57:27 +0100 Subject: [PATCH] =?UTF-8?q?=D0=B2=20=D0=BF=D1=80=D0=BE=D1=86=D0=B5=D1=81?= =?UTF-8?q?=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.svelte | 130 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 89 insertions(+), 41 deletions(-) diff --git a/src/App.svelte b/src/App.svelte index ca6e7e6..b68ce2e 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -254,6 +254,7 @@ if (debug) console.log("[e]", "device list wrong"); } else { socket[ws] = new WebSocket("ws://" + ip + ":81"); + socket.binaryType = "blob"; //socket[ws] = new WebSocket("ws://" + ip + "/ws"); if (debug) console.log("[i]", ip, "started connecting..."); } @@ -280,53 +281,100 @@ //socket[ws].send("HELLO"); }); socket[ws].addEventListener("message", function (event) { - let data = event.data.toString(); + let data; + if (typeof event.data === "string") { + data = event.data; + if (data === configJsonStart) { + if (debug) console.log("[i]", "start receiving configJson"); + configJsonFlag = true; + } + if (data === widgetsJsonStart) { + if (debug) console.log("[i]", "start receiving widgetsJson"); + widgetsJsonFlag = true; + } + } + if (event.data instanceof Blob) { + if (debug) console.log("[i]", "binary frame"); + let reader = new FileReader(); + reader.readAsText(event.data); + reader.onload = () => { + let result = reader.result; + //сборщик configJson пакетов======================================== + if (configJsonFlag) { + configJsonBuf = configJsonBuf + result; + if (result.includes("]")) { + if (debug) console.log("[i]", "stop receiving configJson"); + if (debug) console.log("[i]", configJsonBuf); + if (IsJsonParse(configJsonBuf)) { + configJson = JSON.parse(configJsonBuf); + configJson = configJson; + if (debug) console.log("[i]", "configJson parced!"); + configJsonFlag = false; + } + } + } + //сборщик widgetsJson пакетов======================================== + if (widgetsJsonFlag) { + widgetsJsonBuf = widgetsJsonBuf + result; + if (result.includes("]")) { + if (debug) console.log("[i]", "stop receiving widgetsJson"); + if (debug) console.log("[i]", widgetsJsonBuf); + if (IsJsonParse(widgetsJsonBuf)) { + widgetsJson = JSON.parse(widgetsJsonBuf); + widgetsJson = widgetsJson; + if (debug) console.log("[i]", "widgetsJson parced!"); + widgetsJsonFlag = false; + } + } + } + }; + } //if (debug) console.log("[i]", "data:", data); //if (data.includes("[log]")) { // data = data.replace("[log]", ""); // addCoreMsg(data); //} //сборщик configJson пакетов======================================== - if (data === configJsonStart) { - if (debug) console.log("[i]", "start receiving configJson"); - configJsonFlag = true; - configJsonBuf = []; - configJson = []; - } - if (configJsonFlag && data != configJsonStart && data != configJsonEnd) { - //data = strdecode(data); - configJsonBuf = configJsonBuf + data; - } - if (data === configJsonEnd) { - if (debug) console.log("[i]", "comleted receiving configJson"); - configJsonFlag = false; - if (IsJsonParse(configJsonBuf)) { - configJson = JSON.parse(configJsonBuf); - configJson = configJson; - if (debug) console.log("[i]", "configJson parced!"); - } - configJsonBuf = []; - } - //сборщик widgetsJson пакетов======================================== - if (data === widgetsJsonStart) { - if (debug) console.log("[i]", "start receiving widgetsJson"); - widgetsJsonFlag = true; - widgetsJsonBuf = []; - widgetsJson = []; - } - if (widgetsJsonFlag && data != widgetsJsonStart && data != widgetsJsonEnd) { - widgetsJsonBuf = widgetsJsonBuf + data; - } - if (data === widgetsJsonEnd) { - if (debug) console.log("[i]", "comleted receiving widgetsJson"); - widgetsJsonFlag = false; - if (IsJsonParse(widgetsJsonBuf)) { - widgetsJson = JSON.parse(widgetsJsonBuf); - widgetsJson = widgetsJson; - if (debug) console.log("[i]", "widgetsJson parced!"); - } - widgetsJsonBuf = []; - } + //if (data === configJsonStart) { + // if (debug) console.log("[i]", "start receiving configJson"); + // configJsonFlag = true; + // configJsonBuf = []; + // configJson = []; + //} + //if (configJsonFlag && data != configJsonStart && data != configJsonEnd) { + // configJsonBuf = configJsonBuf + data; + //} + //if (data === configJsonEnd) { + // if (debug) console.log("[i]", "comleted receiving configJson"); + // configJsonFlag = false; + // if (IsJsonParse(configJsonBuf)) { + // configJson = JSON.parse(configJsonBuf); + // configJson = configJson; + // if (debug) console.log("[i]", "configJson parced!"); + // } + // configJsonBuf = []; + //} + ////сборщик widgetsJson пакетов======================================== + //if (data === widgetsJsonStart) { + // if (debug) console.log("[i]", "start receiving widgetsJson"); + // widgetsJsonFlag = true; + // widgetsJsonBuf = []; + // widgetsJson = []; + //} + //if (widgetsJsonFlag && data != widgetsJsonStart && data != widgetsJsonEnd) { + // widgetsJsonBuf = widgetsJsonBuf + data; + //} + //if (data === widgetsJsonEnd) { + // if (debug) console.log("[i]", "comleted receiving widgetsJson"); + // widgetsJsonFlag = false; + // if (IsJsonParse(widgetsJsonBuf)) { + // widgetsJson = JSON.parse(widgetsJsonBuf); + // widgetsJson = widgetsJson; + // if (debug) console.log("[i]", "widgetsJson parced!"); + // } + // widgetsJsonBuf = []; + + // //==================================================================== }); socket[ws].addEventListener("close", (event) => {