diff --git a/src/websocket.js b/src/websocket.js index 4614bd9..e709cd5 100644 --- a/src/websocket.js +++ b/src/websocket.js @@ -87,6 +87,22 @@ function createWebSocket(token, onMessage) { const { data, event, code, timestamp } = response || {}; if (event === "pong") return; + const passthroughEvents = [ + "/addons/shoot/battery", + "/addons/shoot/otaProgress", + "/addons/shoot/otaResult", + ]; + if (passthroughEvents.includes(event)) { + if ( + (code == null || Number(code) === 0) && + onMessage && + data && + typeof data === "object" + ) { + onMessage({ event, data, code, timestamp }); + } + return; + } if (!data || typeof data !== "object") return; if (data.type) { if (ENABLE_REALTIME_MESSAGE_LOG) { @@ -97,27 +113,29 @@ function createWebSocket(token, onMessage) { } if (!Array.isArray(data.updates)) return; if (onMessage) onMessage(data.updates); - const msg = data.updates[0]; - if (msg) { - if (ENABLE_REALTIME_MESSAGE_LOG) { - console.log( - "收到 WebSocket 更新", - getMessageTypeName(msg.constructor), - { updateCount: data.updates.length } - ); - } - if (msg.constructor === MESSAGETYPES.RankUpdate) { - uni.setStorageSync("latestRank", msg.lvl); - } else if (msg.constructor === MESSAGETYPES.LvlUpdate) { - uni.setStorageSync("latestLvl", msg.lvl); - } else if (msg.constructor === MESSAGETYPES.DeviceOnline) { - uni.$emit("update-online", true); - } else if (msg.constructor === MESSAGETYPES.DeviceOffline) { - uni.$emit("update-online", false); - } else if (msg.constructor === MESSAGETYPES.DeviceCharging) { - uni.$emit("device-charging"); - } + const firstMessage = data.updates[0]; + if (firstMessage && ENABLE_REALTIME_MESSAGE_LOG) { + console.log( + "收到 WebSocket 更新", + getMessageTypeName(firstMessage.constructor), + { updateCount: data.updates.length } + ); } + data.updates.forEach((msg) => { + if (msg) { + if (msg.constructor === MESSAGETYPES.RankUpdate) { + uni.setStorageSync("latestRank", msg.lvl); + } else if (msg.constructor === MESSAGETYPES.LvlUpdate) { + uni.setStorageSync("latestLvl", msg.lvl); + } else if (msg.constructor === MESSAGETYPES.DeviceOnline) { + uni.$emit("update-online", true); + } else if (msg.constructor === MESSAGETYPES.DeviceOffline) { + uni.$emit("update-online", false); + } else if (msg.constructor === MESSAGETYPES.DeviceCharging) { + uni.$emit("device-charging"); + } + } + }); }); socketTask.onError((err) => {